Reputation: 7
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
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
Reputation: 727137
The problem in your code can be generally described as "memory management":
scanf
scanf
allows for buffer overrunsstring
as an array of 100 strings, not as a single stringstring[100]
to the search functionTo 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":
break
after return
in the search functioni
before the loop and in the loop\n
at the beginning of a line is discouraged.Upvotes: 0
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
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