Linked list and functions in C

Having some trouble altering the values in a function

Page 1 of 1

7 Replies - 6147 Views - Last Post: 20 September 2007 - 10:10 AM Rate Topic: -----

#1 sacwchiri  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 27-March 07

Linked list and functions in C

Posted 19 September 2007 - 05:02 PM

So i was looking around and some other threads about this and i couldnt get my program to work.

I am creating a linked list and trying to add a set of data to the last one but i cant get the function to alter the last pointer >.<....

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct{
	char data[10];
	struct node *nxtP;
}node;

void addF(node *hdr, char data[10]){
	node *temp;	
	temp = (node*)malloc(sizeof(node));
	strcpy(temp->data, data);
	temp->nxtP = NULL;
	*hdr = *temp;
}

void addR(node **hdr, char data[10]){
	node *temp; 
	node *newN;	
	node *temp2;

	temp = (node*)malloc(sizeof(node));
	temp2 = (node*)malloc(sizeof(node));
	newN = (node*)malloc(sizeof(node));
	
	*temp = **hdr;
	strcpy(newN->data, data);

	temp->nxtP = newN;
	printf("%p - ", newN);
	printf("%p - ", temp->nxtP);

}

int main(){
	node *hdr;
	node *temp;
	node *temp2;
	char data[10];
	int k;
	
	hdr = (node*)malloc(sizeof(node));
	temp = (node*)malloc(sizeof(node));
	temp2 = (node*)malloc(sizeof(node));

	scanf("%s", data);
	addF(hdr, data);
	
	scanf("%s", data);
	addR(&hdr, data);
	printf("%p\n", hdr->nxtP);

	for(*temp2 = *hdr; temp2 != NULL; temp2 = temp2->nxtP)
		printf("%s\n", temp2->data);
	
	free(hdr);
	free(temp2);
	return 0;
}



the adresses in the function tell me that they are not altering the hdr->nxtP part soo what am i doing wrong???

thanx

Is This A Good Question/Topic? 0
  • +

Replies To: Linked list and functions in C

#2 Hyper_Eye  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 116
  • Joined: 13-September 07

Re: Linked list and functions in C

Posted 19 September 2007 - 09:28 PM

Instead of

*temp = **hdr;


use

temp = *hdr;


You are wanting the temp pointer to point to the memory address of hdr. So, set the pointer's equal to each other. Don't try to set the contents equal to each other. I hope that helps.
Was This Post Helpful? 0
  • +
  • -

#3 sacwchiri  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 27-March 07

Re: Linked list and functions in C

Posted 19 September 2007 - 11:14 PM

wow that was the answer. I had completely forgotten about that, thanx :)
Was This Post Helpful? 0
  • +
  • -

#4 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: Linked list and functions in C

Posted 20 September 2007 - 12:12 AM

Also you don't need a cast to malloc in C.
Was This Post Helpful? 0
  • +
  • -

#5 sacwchiri  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 27-March 07

Re: Linked list and functions in C

Posted 20 September 2007 - 06:38 AM

what?? why not? i thought you needed to allocate the memory you were going to use before this whole process???
Was This Post Helpful? 0
  • +
  • -

#6 Hyper_Eye  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 116
  • Joined: 13-September 07

Re: Linked list and functions in C

Posted 20 September 2007 - 09:54 AM

View Postsacwchiri, on 20 Sep, 2007 - 08:38 AM, said:

what?? why not? i thought you needed to allocate the memory you were going to use before this whole process???


You do need to allocate memory. I believe he is saying you don't have to typecast a malloc in C. That may be true in most implementations of malloc but I have run into cases where not typecasting on a malloc will at least give you warnings on compile. I don't remember exactly what I was compiling on at the time but I have been typecasting malloc for a long time. It definitely doesn't hurt anything.

You are allocating memory in places where you don't need to. A good example is in your addR function when you allocate memory for temp. That is a waste. After doing that you set temp equal to a pointer that already (hopefully) has memory allocated (hdr). You don't have to allocate memory every time you use a pointer. It depends on what you are using it for.

Really there are a lot of things in this code that are either wrong or unconventional. It seems that your grasp on pointers is not as strong as it needs to be. Since linked lists rely heavily on pointers I would recommend that you revisit pointers and get a little better understanding of them before continuing with linked lists. In particular you need to strengthen your understanding of how to manipulate data in them, access the data within them, reference them, how to pass them to functions, and how to maintain memory (malloc, realloc, free) when dealing with pointers.
Was This Post Helpful? 0
  • +
  • -

#7 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: Linked list and functions in C

Posted 20 September 2007 - 10:01 AM

View PostHyper_Eye, on 20 Sep, 2007 - 10:24 PM, said:

View Postsacwchiri, on 20 Sep, 2007 - 08:38 AM, said:

what?? why not? i thought you needed to allocate the memory you were going to use before this whole process???


You do need to allocate memory. I believe he is saying you don't have to typecast a malloc in C. That may be true in most implementations of malloc but I have run into cases where not typecasting on a malloc will at least give you warnings on compile. I don't remember exactly what I was compiling on at the time but I have been typecasting malloc for a long time. It definitely doesn't hurt anything.

ANSI C allows safe conversion from void* to any pointer type except function pointer. Every standard conforming implementation should follow it. Cast is only required in C++.
Was This Post Helpful? 0
  • +
  • -

#8 Hyper_Eye  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 116
  • Joined: 13-September 07

Re: Linked list and functions in C

Posted 20 September 2007 - 10:10 AM

View PostXing, on 20 Sep, 2007 - 12:01 PM, said:

ANSI C allows safe conversion from void* to any pointer type except function pointer. Every standard conforming implementation should follow it. Cast is only required in C++.


You know not all compilers and environments conform to ANSI C standards. I work with professional C developers every day and they all typecast a malloc. I am not trying to say that you are wrong as you are absolutely right. But, I think you are more likely to see malloc typecasted then not. Even a lot of C books will typecast a malloc. Besides, if it has to be done in C++ (and this could be where I got compile errors when not typecasting malloc) then why not just typecast malloc in either so it becomes a part of your style? Then you don't have to remember to do it in C++.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1