# Find a String in an Array

This program is used to get names and numbers of N person, print out the numbers of a person. Please help me improve this code.

To avoid a very long name that exceeds char's size, I limit it to 255. Is there a better way?

To avoid buffer, I included a space before % sign when using scanf and not use getchar(). Is there a better way?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// Limiting string input
const unsigned int STRINGCONSTRAINT = 255;

// Person struct to store name and numbers
typedef struct {
char name[STRINGCONSTRAINT];
char numbers[STRINGCONSTRAINT];
} person;

void printNumber(person people[], int size, char personFind[]);

// Asked for array's size and a person's name to find
int main(void) {
unsigned int size = 0;
printf("Enter the amount of people: ");
scanf(" %d", &size);

person people[size];

char personFind[STRINGCONSTRAINT];
puts("Who do you want to find?");
printf("Person to find: ");
scanf(" %[^\n]s", personFind);

printNumber(people, size, personFind);

return 0;
}

// Get name and numbers for the array
void getNameNumber(person people[], int size) {
unsigned int i;
for (i = 0; i < size; i++) {
printf("Name: ");
scanf(" %[^\n]s", people[i].name);
printf("Numbers: ");
scanf(" %[^\n]s", people[i].numbers);
}
}

// Find the person and print the numbers
void printNumber(person people[], int size, char personFind[]) {
unsigned int i;
int flag = 1;

for (i = 0; i < size; i++) {
flag = strcmp(people[i].name, personFind);
if (flag == 0) {
puts("Found!");
printf("You can call %s at %s.\n", personFind, people[i].numbers);
}
}
}


## static functions

Both of your getNameNumber and printNumber should be made static, because I doubt that you plan to export them.

## Simpler functions

printf("Enter the amount of people: ");


can simply use puts, which doesn't have any formatting code in it. You use puts below, so this would also help with consistency.

## Stack allocation

person people[size];


is not available in all versions of C, since size is not a constant. For best portability you'd want to malloc this, or perhaps alloca if you feel strongly that the memory should be in the stack and not on the heap.

## const

printNumber's array arguments should be const, since you don't mutate them.

To avoid a very long name that exceeds char's size, I limit it to 255. Is there a better way?

For your use case, not really. A fixed array is the most appropriate. However, your code is vulnerable to overflows. You should be putting a width in your format specifier.

To avoid buffer, I included a space before % sign and not use getchar(). Is there a better way?

I'm not sure which usage of % you're referring to, but they all look fairly sane, so no; there's not really a better way.

• "is not available in all versions of C, since size is not a constant". VLA available in C99 and since C11 compiler might not support VLA (in this case macro constant __STDC_NO_VLA__ equals 1). – eanmos Oct 16 at 4:22
• Wasn't puts the function that adds a newline, in contrast to fputs? Therefore it is not a good replacement for this printf call. – Roland Illig Oct 16 at 4:24
• "can simply use puts" --> not same functionality. – chux - Reinstate Monica Oct 26 at 9:38

Strange format

The s in scanf(" %[^\n]s", personFind) serves no purpose. Use " %[^\n]" instead

... or better, use a width limit. See scanf / field lengths

Consider this line:

  scanf(" %d", &size);


We throw away the return value from this function, which means we have no way of knowing whether a value was written to size. In this case, we don't have undefined behaviour, because we initialised it to zero before calling scanf(), but that's not the case later in the program where we read strings into uninitialized memory. In any case, it's rude to users to silently change invalid input into a default answer, rather than giving a meaningful error message.

BTW, the space at the beginning of that conversion is redundant - numeric conversions always skip leading whitespace.