Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
C: typedef struct ??
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Unsupported Software
View previous topic :: View next topic  
Author Message
derverstand
Guru
Guru


Joined: 15 Dec 2005
Posts: 511
Location: /dev/null

PostPosted: Wed Aug 09, 2006 11:29 am    Post subject: C: typedef struct ?? Reply with quote

Hi,

I want to setup a simple linked list in C. So I have to do something like this:
Code:

struct s_entry{
   int value;
   struct s_entry *next;
};
typedef struct entry t_entry;

... // Some Code

t_entry entry;

... // Allocation etc



And now the big ?
Why can't I access a value like this?
Code:

entry.value

It only works with
Code:

entry->value


Whats the difference exaclty? And is it possible to write the "->" expression with "." ?

Best regards.
Back to top
View user's profile Send private message
tboloo
Guru
Guru


Joined: 20 Jan 2006
Posts: 403
Location: Grodzisq, Poland

PostPosted: Wed Aug 09, 2006 11:50 am    Post subject: Reply with quote

Hi.
Looks like you've made some mistakes - maybe you dynamically allocated memory for your struct ?If so, you'll need to dereference pointer in order to use . instead of ->, like
Code:
*(entry.value) = 1;

Anyway this code below works fine
Code:

#include <stdio.h>

struct s_entry{
        int value;
        struct s_entry* next;
};

typedef struct s_entry t_entry;

int main()
{
        t_entry entry;
        entry.value = 1;
        entry.next = NULL;
        printf(" %d, %p", entry.value, entry.next );
        printf("\n");
        return 0;
}

_________________
The clock is ticking, brothers and sisters, counting down to Armageddon.
Back to top
View user's profile Send private message
Archangel1
Veteran
Veteran


Joined: 21 Apr 2004
Posts: 1212
Location: Work

PostPosted: Wed Aug 09, 2006 10:47 pm    Post subject: Re: C: typedef struct ?? Reply with quote

derverstand wrote:
Whats the difference exaclty? And is it possible to write the "->" expression with "." ?

Basically -> is for a pointer, . is when the variable actually is a struct. So:
Code:

t_entry spam;
t_entry* eggs = malloc(sizeof(t_entry));

spam.value = 5;
eggs->value = 5;

_________________
What are you, stupid?
Back to top
View user's profile Send private message
Voltago
Advocate
Advocate


Joined: 02 Sep 2003
Posts: 2593
Location: userland

PostPosted: Wed Aug 09, 2006 10:54 pm    Post subject: Reply with quote

You can even do this:

Code:
typedef struct s_entry t_entry;

struct s_entry{
        int value;
        t_entry* next;
};
Back to top
View user's profile Send private message
voytas
Apprentice
Apprentice


Joined: 31 Mar 2004
Posts: 203
Location: Poland, Lodz

PostPosted: Thu Aug 10, 2006 9:00 am    Post subject: Reply with quote

if A is a pointer to a struct these are equivalents:
Code:
(*A).val = 10;
A->val = 10;

_________________
LAPTOP: ThinkPad T530
Back to top
View user's profile Send private message
tboloo
Guru
Guru


Joined: 20 Jan 2006
Posts: 403
Location: Grodzisq, Poland

PostPosted: Thu Aug 17, 2006 10:34 am    Post subject: Reply with quote

voytas wrote:
if A is a pointer to a struct these are equivalents:
Code:
(*A).val = 10;
A->val = 10;

You're right. I've put right comma in a wrong place :oops:
_________________
The clock is ticking, brothers and sisters, counting down to Armageddon.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Unsupported Software All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum