Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

gets()

Status
Not open for further replies.

gander

Programmer
Aug 11, 2003
5
0
0
US
HI just a quickie
whats wrong with the gets function here

viod main()
{
int i,j, persons = 10;
char name[11],
*c_ptr;

c_ptr = name;

for(i=0;i<persons;i++)
{
printf(&quot;\nEnter name for person %d&quot;,i+1);
gets(c_ptr);
}

// it wont let me input a name for person 1


for(j=0;j<persons;j++)
puts(c_ptr);

}
 
I hope this makes sense, if not, post and I'll try to be more clear. Also, if this is not what you intend to do, please don't take it as condecending or an insult, just post your logic and what you think it should do, and what it is actually doing. I'm making an inference b/c you didn't really post enough info. That being said, try this:

I don't understand your logic. Are you trying to put 11 names into the array char[11]? If so, that's not right. If you are, you need to have a 2d array.

The array char[11] is 11 contiguious characters in memroy, and the statment c_ptr = name; assigns c_ptr to the first character in the array.
The array char[11] is not 11 pointers to characters, it's a pointer to 11 characters, and the gets(c_ptr) fills up to 11 characters of the array. So, essientally you are overwriting the same array every time you run the for loop, and the very last person is left in the array. Also, if the name is more than 11 characters long, the array will be overfilled, but I think that gets will return a null pointer.

If you want an arrray of 11 names, you need to have 11 pointers to characters, not a pointer to 11 characters, which is what the array is. As always, I hope that helped!

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
You need to have something like this (if I understand your intentions correctly):

#define NUM_PEOPLE 10
#define MAX_NAME_LEN 30

char people[NUM_PEOPLE][MAX_NAME_LEN+1]; /* 10 people with max name length of 30 */

/* ... */

int i;
char *tmp;

for (i=0;i<NUM_PEOPLE;++i) {
printf(&quot;Enter a name for person %d\n&quot;,i);
if (NULL==fgets(people,MAX_NAME_LEN,stdin)) {
/* fgets() failed, handle error */
} else {
tmp=strchr(people,'\n');
if (NULL!=tmp) {
*tmp='\0';
} else {
/* line truncated, handle accordingly */
}
}
}

Note that the example uses fgets() instead of gets(). Any usage of gets() is a bug as there's no way to prevent someone from crashing your program by typing in more characters than your buffer can handle. The 2nd argument to fgets() prevents this as fgets() will only place this many characters in your buffer.

HTH,

Russ
bobbitts@hotmail.com
 
hi gander,
ur logic is right but instead of gets() use getchar(). Ur first for loop should contain statements such as :

for(i=0;i!='\0';)
{
name=getchar();
i++;
}

gets() should be used to get a whole string at once. Instead of ur first for loop, u can use gets(name) to read the name from the user.

Hope this solves ur problem.

Shri.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top