473,326 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Pointers to struct

Hi guys,

Since I'm a newbie in C programming I want some suggestions with the interpretation for the pointer usage. So I have the following piece of code (is not my implementation). What I don't understand here is why it is is used the expression "*p = n; "?

As long as I know if I call the function with list_add(&n,1) means that I transfer the address of the node "n" to the function list_add.

Now it means that *p = &n or *p = n (this are similar construction). This means if &n=0x030402ff ----> *p = 0x030402ff. Right?

What I don't understand is why if I comment line " *p = n; " the value from *p is always 0x0 (the address of the NULL element).

typedef struct ns {
int data;
struct ns *next;
} node;

node *list_add(node **p, int i) {
/* some compilers don't require a cast of return value for malloc */
node *n = (node *)malloc(sizeof(node));
if (n == NULL)
return NULL;
n->next = *p;
*p = n; <<--------------------------?????
n->data = i;
return n;
}

int main(void) {
node *n = NULL;
list_add(&n, 0); /* list: 0 */
list_add(&n, 1); /* list: 1 0 */
return 0;
}

Sorry for bugging you with this simple questions.

Thanks in advance
Dec 12 '07 #1
5 1635
femina
35
i think your statement that follows like Now it means that *p = &n or *p = n (this are similar construction) is wrong. i jus guess and am not sure.
because n is a pointer of type node(structure)
&n means the address in which n is located say 5000 and
n in turn contains a value that is the address of some other node may be
anyway please check with the answer





Hi guys,

Since I'm a newbie in C programming I want some suggestions with the interpretation for the pointer usage. So I have the following piece of code (is not my implementation). What I don't understand here is why it is is used the expression "*p = n; "?

As long as I know if I call the function with list_add(&n,1) means that I transfer the address of the node "n" to the function list_add.

Now it means that *p = &n or *p = n (this are similar construction). This means if &n=0x030402ff ----> *p = 0x030402ff. Right?


What I don't understand is why if I comment line " *p = n; " the value from *p is always 0x0 (the address of the NULL element).

typedef struct ns {
int data;
struct ns *next;
} node;

node *list_add(node **p, int i) {
/* some compilers don't require a cast of return value for malloc */
node *n = (node *)malloc(sizeof(node));
if (n == NULL)
return NULL;
n->next = *p;
*p = n; <<--------------------------?????
n->data = i;
return n;
}

int main(void) {
node *n = NULL;
list_add(&n, 0); /* list: 0 */
list_add(&n, 1); /* list: 1 0 */
return 0;
}

Sorry for bugging you with this simple questions.

Thanks in advance
Dec 12 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
You never do anything with the return value from list_add in main(). That mwenas the n in main() never gets changed. Hence, every call to list_add is using the same value of n.

The *p = n is how you update the n in main().

Another way is:
Expand|Select|Wrap|Line Numbers
  1. main(void) {
  2. node *n = NULL;
  3. n = list_add(&n, 0); /* list: 0 */
  4. n =  list_add(&n, 1); /* list: 1 0 */
  5. return 0;
  6. }
  7.  
Dec 12 '07 #3
Thanks for answer. You are right but I still don't understand why "n" is not changing. The function is called by reference, right?

I think the confusion come from the fact that in the fuction another *n is created. Is the pointer returned by this function the one which points the new structure created inside the function?

Thanks in advance.
Dec 13 '07 #4
femina
35
the function is not called by reference.
the address of the first argument is passed (pass by address)
and the second argument is pass by value

Thanks for answer. You are right but I still don't understand why "n" is not changing. The function is called by reference, right?

I think the confusion come from the fact that in the fuction another *n is created. Is the pointer returned by this function the one which points the new structure created inside the function?

Thanks in advance.
Dec 13 '07 #5
You never do anything with the return value from list_add in main(). That mwenas the n in main() never gets changed. Hence, every call to list_add is using the same value of n.

The *p = n is how you update the n in main().

Another way is:
Expand|Select|Wrap|Line Numbers
  1. main(void) {
  2. node *n = NULL;
  3. n = list_add(&n, 0); /* list: 0 */
  4. n =  list_add(&n, 1); /* list: 1 0 */
  5. return 0;
  6. }
  7.  

Hi,

I really don't understand what is happening when the function returns:

node *list_add(node **p, int i) {
/* some compilers don't require a cast of return value for malloc */
node *n = (node *)malloc(sizeof(node));
if (n == NULL)
return NULL;
n->next = *p;
*p = n;
n->data = i;
return n;
}

As far as I can see seems that local pointer *p never get's destroyed.

Can you please tell me if I'm wrong or right in my judgement:
So: when the the function is called with: list_add(&n, 0); means that pointer *p is declared but it doesn't have any space allocated in the memory (stack or heap??) and when *p = n; means that memory is allocated?Right?

What's happening when the function return? the *p pointer is freed or not?

Thanks. I really want to understand this mechanism (maybe you can provide me a useful link with a description about this issue)
Dec 14 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Thomas Matthews | last post by:
Hi, Given a structure of pointers: struct Example_Struct { unsigned char * ptr_buffer; unsigned int * ptr_numbers; }; And a function that will accept the structure:
3
by: Christian F | last post by:
Hi, I'm a C-newbie and I would like to know if I am doing something wrong in the code below. It is working, but I'm afraid it might not be correct because I don't really understand everything of...
20
by: fix | last post by:
Hi all, I feel unclear about what my code is doing, although it works but I am not sure if there is any possible bug, please help me to verify it. This is a trie node (just similar to tree nodes)...
8
by: John Hanley | last post by:
I working in C. I haven't paid much attention to void pointers in the past, but I am wondering if I can use them for my various linked lists to save work. I have two different linked lists that...
7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
8
by: Steve Lambert | last post by:
Hi, I'd be grateful if someone could clarify this for me. In the linked list structure my intention is to declare an array of length 3 containing pointers to node eg. Node *Iterators The...
13
by: brian | last post by:
Quick question: if I have a structure: struct foo { unsigned char *packet; unsigned char *ip_src; };
42
by: x-pander | last post by:
Is is guaranteed, that a pointer to any object in C points exactly at the lowest addressed byte of this object? Specifcally is it possible for any platform/os/compiler combination that: (char...
6
by: paudirac | last post by:
Hi, I need to maintain N linked lists where N is determined at runtime. The linked lists are defined as follows, struct linked_list { int data; struct linked_list next; }
47
by: sunglo | last post by:
Some time a go, in a discussion here in comp.lang.c, I learnt that it's better not to use a (sometype **) where a (void **) is expected (using a cast). Part of the discussion boiled down to the...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.