LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   array of pointers to struct in c : how? (https://www.linuxquestions.org/questions/programming-9/array-of-pointers-to-struct-in-c-how-502496/)

moob8 11-17-2006 08:24 AM

array of pointers to struct in c : how?
 
So I'm writing a program in straight c (gcc compiler in slackware 11).
Code:

struct vox_node {
  char *var_name;        /* name of this sylable/phoneme */
  boolean is_sus;        /* sustaining xor not? */
  int size;              /* length of sample in samples (this is the wavelength
                            if it is a sustaining sound) */
  WORD *sound;            /* the actual sample (16 bit word) */
  struct vox_node *next;  /* guess */
};

Later on (global variable):
Code:

struct vox_node *p_table[4];
Later on (in a subroutine):
Code:

  p_table = calloc(sizeof(struct vox_node), 4);
later still (call this "snippet 1"):
Code:

      p_table[1] = (struct phoneme_node *) NULL;
"snippet 2" (in main):
Code:

  p_table = (struct vox_node *) calloc(sizeof(struct vox_node), 4);
Snippet 1 generates this error on compile:
Code:

warning: assignment from incompatible pointer type
Snippet 2 generates this error on compile:
Code:

error: incompatible types in assignment
What is the minimal change needed to force the above to compile without error/warning?

I've had this sort of problem before and in the past I've fixed it by doing very evil things with void and char and in generally being very architecture-dependent and nonportable. This time I'd like to use the "proper" way.

Thank you in advance.

mjones490 11-17-2006 08:51 AM

Code:

struct vox_node *p_table[4];
Here, you are creating a table of pointers, the table having four elements. So,
Code:

p_table = calloc(sizeof(struct vox_node), 4);
or
Code:

p_table = (struct vox_node *) calloc(sizeof(struct vox_node), 4);
are not needed. You will, however, need to create each node individually, like so:
Code:

p_table[1] = (struct vox_node *) calloc(sizeof(struct vox_node), 1);
Hope this helps.

sundialsvcs 11-17-2006 09:15 AM

The hardest thing about doing fancy structures in "C" is visualizing what you are trying to do. Sit down with a legal-pad and a number-two pencil and draw it out.

moob8 11-17-2006 05:16 PM

Quote:

Originally Posted by sundialsvcs
The hardest thing about doing fancy structures in "C" is visualizing what you are trying to do. Sit down with a legal-pad and a number-two pencil and draw it out.

Well, I didn't do that. But I did something better. An old lesson I keep forgetting: when in doubt, delete the whole mess and start over. So my original question is now no longer relevant to my problem. Thanks for the quick replies both of you though! :D

xhi 11-18-2006 09:40 AM

> An old lesson I keep forgetting: when in doubt, delete the whole mess and start over.

thats a lesson? i guess it works while you are at the stage where you do things wrong the first time. however it seems like it might be a little counter productive once you start doing things right. might be worthwhile to come up with a new lesson. :)

indienick 11-18-2006 09:20 PM

I agree with moob8, there are so many times I've been strung out on a piece of code not working, and the best way (IMO) is to delete and start fresh.

Levia8an 11-21-2006 11:13 AM

hi everyone. It's my first post here :)

1.
Quote:

p_table = calloc(sizeof(struct vox_node), 4);
This line is erroneous . p_table is a table name and behaves like a constant pointer i.e. you cannot assign values to p_table

2.
Quote:

p_table[1] = (struct phoneme_node *) NULL;
p_table[1] is of type pointer to a vox_node struct .Thus it makes sense the "incompatible pointer type " warning

3.
Quote:

p_table = (struct vox_node *) calloc(sizeof(struct vox_node), 4);
similarly to 1. you cannot assign any value to p_table ;

Hope this is not misleading.If wrong please correct me


All times are GMT -5. The time now is 01:05 PM.