1

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;

}

1
  • Please format your code in a readable manner. Commented Feb 17, 2013 at 11:54

4 Answers 4

2

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 &.

Sign up to request clarification or add additional context in comments.

2 Comments

PS: return i; break; makes no sense.
@m0skit0 Haha, didn't see that one, rich!
0

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.

1 Comment

Thank you, it can work already. I have weak knowledge about the memory management issues, which is why I was wondering what was wrong in my coding. Thank you for your help =)
0

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);
}

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.