Learning C: Writing a Linked List Problem

  • Thread starter sneez
  • Start date
  • Tags
    Function
In summary, the conversation discusses the use of strcpy and pointer variables in the context of a C program. The individual is trying to learn C after Java and is currently writing a linked list with a problem in the add function. They are trying to get input from the user, but the program goes to the next line without allowing for input. Others suggest using fflush(stdin) or checking for newlines in the buffer. The conversation also mentions the importance of checking bounds and avoiding buffer overflow problems when using strcpy. Finally, the conversation discusses the differences between assigning pointers and copying data, emphasizing the need to use strcpy to avoid memory leaks.
  • #1
sneez
312
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
  • #2
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
 
  • #3
*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
 
  • #4
Just a note: Whenever doing strcpy operations make sure to check bounds. This is how you create buffer overflow problems.
 
  • #5
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 ?
 
  • #6
tail->name and str are both pointers (char *).
so
tail->name = str;
means assign the adress that str points to to the pointer variable tail->name, so that that pointer variable points to the same adress. 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()
 
  • #7
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.
 
  • #8
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.
 

1. What is a linked list?

A linked list is a data structure in which each element contains a pointer to the next element in the list, forming a sequence. It allows for dynamic allocation of memory and efficient insertion and deletion of elements.

2. Why is learning to write a linked list important for learning C?

Learning to write a linked list is important for learning C because it helps to develop important skills such as memory management, pointer manipulation, and algorithmic thinking. These skills are essential for understanding and mastering the C programming language.

3. What are the basic steps for writing a linked list in C?

The basic steps for writing a linked list in C are as follows:

  • Define a structure for the list node, which contains the data and a pointer to the next node.
  • Create functions for creating a new node, inserting a new node, and deleting a node.
  • Initialize the head pointer to NULL.
  • Use the functions to manipulate the linked list.

4. How do you access and traverse a linked list in C?

To access and traverse a linked list in C, you can use a pointer variable to traverse the list starting from the head node. You can use a while loop to iterate through the list, moving the pointer to the next node until it reaches the end of the list.

5. What are the advantages and disadvantages of using a linked list in C?

The advantages of using a linked list in C include efficient insertion and deletion of elements, dynamic memory allocation, and flexibility in size. The disadvantages include slower access time compared to arrays, extra memory required for the pointers, and difficulty in searching for a specific element in the list.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
949
Replies
7
Views
244
  • Programming and Computer Science
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
833
  • Set Theory, Logic, Probability, Statistics
2
Replies
54
Views
4K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
4
Replies
107
Views
5K
  • Computing and Technology
Replies
8
Views
2K
  • Programming and Computer Science
Replies
4
Views
345
  • STEM Academic Advising
Replies
3
Views
925
Back
Top