LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 11-17-2006, 08:24 AM   #1
moob8
Member
 
Registered: Sep 2006
Distribution: slackware
Posts: 132

Rep: Reputation: 15
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.
 
Old 11-17-2006, 08:51 AM   #2
mjones490
Member
 
Registered: Sep 2005
Distribution: LFS
Posts: 60

Rep: Reputation: 22
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.
 
Old 11-17-2006, 09:15 AM   #3
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,381

Rep: Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109
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.
 
Old 11-17-2006, 05:16 PM   #4
moob8
Member
 
Registered: Sep 2006
Distribution: slackware
Posts: 132

Original Poster
Rep: Reputation: 15
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!
 
Old 11-18-2006, 09:40 AM   #5
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
> 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.
 
Old 11-18-2006, 09:20 PM   #6
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
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.
 
Old 11-21-2006, 11:13 AM   #7
Levia8an
LQ Newbie
 
Registered: Nov 2006
Location: Greece
Distribution: Fedora Core 5
Posts: 1

Rep: Reputation: 0
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
I just want to make sure this is correct, struct pointer array paramter in function. RHLinuxGUY Programming 6 08-20-2006 09:30 PM
c array of pointers greg108 Programming 10 08-17-2005 10:25 AM
array of char pointers djgerbavore Programming 2 01-08-2005 01:59 PM
a question about array pointers in C veritas Programming 12 07-18-2004 06:31 PM
array of struct ChimpFace9000 Programming 1 06-12-2002 08:14 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration