LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-30-2007, 02:11 PM   #1
shogun1234
Member
 
Registered: May 2004
Posts: 226

Rep: Reputation: 15
assign int to a struct member


I am newbie to the c programming and am confused with the concepts.
I create a struct c_list. Then I want to assign an int to the struct member id with head->id = 10. However, after using printf function, the output is something like "id value: 85472". Why doesn't it print 10 directly? Where did I do it wrong?

Thanks in advice,

The source is as below.

Code:
#include <stdlib.h>
#include <stdio.h>
struct c_list{
        struct c_list *next;
        int id;
};
void
init(struct c_list *head)
{
        head = malloc(sizeof(struct c_list));
        //head->next = head;
        head->id = 10;
}

int main(int argc, char **argv)
{
        struct c_list *head;
        init(head);
        printf("id value: %d\n", head->id );
        return 0;
}
 
Old 07-30-2007, 02:18 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
You can see what the pointer (head) is in the main function and in the init. You'll see that you do not have the allocated structure in main, but just the default value (it's better to set head to NULL at the beginning, that will show you it is not what you want). To get the allocated structure to main you can either use struct c_list **head for init or return struct c_list *.

Corrected code:
Code:
struct c_list *
init(void)
{
        struct c_list *head = malloc(sizeof(struct c_list));
        /* you should check for NULL here */
        //head->next = head;
        head->id = 10;
        return head;
}

int main(int argc, char **argv)
{
        struct c_list *head = NULL;
        head = init();
        if (head)
                printf("id value: %d\n", head->id );
        return 0;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
access struct member error true_atlantis Programming 4 04-25-2006 05:00 PM
struct member alignment wmoti Programming 2 10-10-2005 05:24 AM
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 10:16 PM
switch statement converting struct char to struct int oceaneyes2 Programming 2 12-10-2003 04:30 PM
problems copying int a struct raven Programming 8 07-21-2002 10:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration