user2080330
user2080330

Reputation: 7

Search string in an array and return index

I have a problem in my code. After I enter the string I want to search, the program crash.

I have checked my code, but I still could not figure out what went wrong.

Would need your advice.

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

int findTarget(char *string, char *nameptr[], int num);
int main()
{
int index, index2;
int size;
char *nameptr[100];
char *string[100];

printf("Enter the number of names: ");
scanf("%d",&size);

for(index=0; index<size; index++)
{
    printf("Enter A Name: ");
    scanf("%s", &nameptr[index]);
}

printf("\nEnter a string to search:");
scanf("%s", &string);

index2 = findTarget(string[100], nameptr, size);

if ( index2 == -1 )
{
  printf("\nNo - no such name\n");
}
else
{
  printf("\nYes - matched index location at %d\n", index2);
 }
return 0;

}

 int findTarget(char *string, char *nameptr[], int num)
 {
int i=0;

for ( i = 0 ; i < num ; i++ )
{

    if (strcmp(nameptr[i],string)==0)
    {
        return i;
        break;
    }
}

return -1;

}

Upvotes: 1

Views: 274

Answers (4)

Gabriel Martins
Gabriel Martins

Reputation: 3

Well, the crashing reason it's because you have not allocated memory for your strings.

Use char nameprt[100]; instead of *char nameptr[100];. What you have declared was an array of pointers to chars and not an array of 100 chars.

And in the scanf you must do like this scanf("%s", nameptr); Reason being, scanf expects a pointer to an array. So there is no reason for the & if the variable is already a pointer.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727137

The problem in your code can be generally described as "memory management":

  • You do not allocate memory for individual strings
  • You are passing addresses of wrong things to scanf
  • Your use of scanf allows for buffer overruns
  • You declared string as an array of 100 strings, not as a single string
  • You are passing string[100] to the search function

To fix this, you need to allocate the individual string dynamically using malloc. You can use a temporary buffer and then copy with strdup, or pre-allocate 100 characters, and limit the scanf to it.

Here is a portion of your program that needs changing:

char *nameptr[100];
char string[100]; // The asterisk is gone

printf("Enter the number of names: ");
scanf("%d",&size);

for(index=0; index<size; index++) {
    char buf[100];
    printf("Enter A Name: ");
    scanf("%99s", buf); // Note the limit of 99
    buf[99] = '\0'; // Just to make sure it's terminated
    nameptr[index] = strdup(buf);
}

printf("\nEnter a string to search:");
scanf("%99s", string); // No ampersand

index2 = findTarget(string, nameptr, size); // string, not string[100]

for (index=0; index<size; index++) {
    free(names[i]);
}

The rest would be just "points for style":

  • You do not need a break after return in the search function
  • You do not need to initialize i before the loop and in the loop
  • Printing \n at the beginning of a line is discouraged.

Upvotes: 0

Javier Villa
Javier Villa

Reputation: 159

You have an array of char*, but when you do a scanf into it, you don't actually have allocated a buffer.

Instead, you should first reserve memory for the buffers via something like:

for(i=0; i<100; i++) {

    nameptr[i] = malloc(STRING_BUFFER_SIZE);
}

Upvotes: 0

cnicutar
cnicutar

Reputation: 182794

You never allocated memory to &nameptr[index], so using it in scanf is undefined behavior. You should try doing a malloc before calling scanf. Also, you should drop the &.

Upvotes: 2

Related Questions