Learning C: Writing a Linked List Problem

  • Thread starter Thread starter sneez
  • Start date Start date
  • Tags Tags
    Function
Click For Summary

Discussion Overview

The discussion revolves around issues encountered while writing a linked list in C, specifically related to user input handling and memory management. Participants explore problems with reading input, pointer assignments, and the implications of using certain functions like `gets` and `strcpy`.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes a problem where the program skips input for a name, suggesting that a newline character might still be in the buffer.
  • Another participant proposes a method to check for newlines in the buffer by printing surrounding text with `gets()`.
  • Some participants mention the use of `fflush(stdin)` as a potential solution, though they note it is not guaranteed to work.
  • Concerns are raised about buffer overflows when using `strcpy`, emphasizing the need to check bounds during string operations.
  • A participant questions why assigning a pointer directly (e.g., `tail->name = str`) does not work as expected, leading to a discussion about pointer behavior and memory allocation.
  • It is suggested that using `strcpy` is necessary to copy the content of a string rather than just assigning the pointer, which could lead to memory issues if the original string goes out of scope.
  • Another participant reiterates the importance of avoiding `gets` due to similar risks of buffer overflow.

Areas of Agreement / Disagreement

Participants generally agree on the risks associated with using `gets` and the importance of checking bounds with `strcpy`. However, there is no consensus on the best way to handle input and memory management, as different methods and their implications are discussed.

Contextual Notes

Limitations include unresolved assumptions about buffer states and the behavior of pointer assignments. The discussion does not reach a definitive resolution on the best practices for handling user input and memory in C.

Who May Find This Useful

This discussion may be useful for individuals learning C programming, particularly those interested in linked list implementations and memory management practices.

sneez
Messages
312
Reaction score
0
Im trying to learn some c after java.

Im writing a linked list and i have this problem

from main i call : add( (x *)&head, (x *)&tail)

inside the function add i try to get input from user

blah balh
strcpy(tail->name, gets(str));


why does the program goes to next line without letting me to input the name ?

i hope I am clear if not let me know

sneez
 
Computer science news on Phys.org
probably there is still a newline in the buffer, to find out you can do this:

blah balh
printf("ABC %s DEF", gets());
strcpy(tail->name, gets(str));

if ABC and DEF get printed on different lines you know for sure
 
*agrees with gerben*
This has happened with me one times too many.
If you are sure that at no point in your program, a new line is making its way in, then try using fflush(stdin) . Ofcourse for some reasons, this is not guaranteed to work.

-- AI
 
Just a note: Whenever doing strcpy operations make sure to check bounds. This is how you create buffer overflow problems.
 
thanx a lot

one more seemly unrelated question.

char *str="blabh";
Why tail->name=str //does not work ?

why do i have to use the strcpy ?
 
tail->name and str are both pointers (char *).
so
tail->name = str;
means assign the address that str points to to the pointer variable tail->name, so that that pointer variable points to the same address. So you will get a problem when the memory that str points to gets freed.

Probably both pointers (str and tail->name) already have some declared memory to which they point and you just want to copy the content of the memory to which str points to the memory that tail->name points to.
You could do this:
*(tail->name) = *str;
but that will only copy the first character of the string, so you should use strcpy()
 
dduardo said:
Just a note: Whenever doing strcpy operations make sure to check bounds. This is how you create buffer overflow problems.

Of course you should also never use gets for the same reason.
 
sneez said:
thanx a lot

one more seemly unrelated question.

char *str="blabh";
Why tail->name=str //does not work ?

why do i have to use the strcpy ?

first of all str should be declare:
char str[] = "blabh"
to ensure the compiler generate the correct code to allocate memory for the array and not just a char pointer.

tail->name = str;
tail->name and str point to the same data in memory, if str is a local variable then that memory is gone when the function exit and tail->name is no longer valid. that is why you need you need to copy the data into tail->name location. Another danger of tail->name = str is that now you just lost the memory location of where tail->name was pointed to earlier, and the memory location allocate for tail->name is now loss. You just have cause a memory leak.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 10 ·
Replies
10
Views
956
  • · Replies 104 ·
4
Replies
104
Views
7K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 21 ·
Replies
21
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 17 ·
Replies
17
Views
2K