GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-May-2008, 21:13
tskd tskd is offline
New Member
 
Join Date: May 2008
Posts: 3
tskd is on a distinguished road

Problem with arrays declared in a structure


Hi,

I have a structure as part of my code.


CPP / C++ / C Code:

typedef struct CAD {

uint8_t rrc;
uint8_t rd;
// pointers to uint8's in an array
uint8_t *rrgn;
uint8_t *rdi;
uint8_t *rv;
} CAD;


I then try to allocate memory for these arrays and give them predetermined values.

CPP / C++ / C Code:
CAD *c_a_d;
c_a_d->rrc = 0x01;
c_a_d->rd = 0x01;
c_a_d->rrgn = malloc(sizeof(uint8_t) * c_a_d->rrc); /*contains as many array elements as the integer c_a_d->rrc */
c_a_d->rrgn = {0x01};
c_a_d->rdi = malloc(sizeof(uint8_t) * c_a_d->rd);
c_a_d->rdi = {0x01};
c_a_d->rrgn = malloc(sizeof(uint8_t) * c_a_d->rd);
c_a_d->rv = {0x03};


The errors I get are in the 3 assignment statements for the arrays.
THE ERROR EXACTLY IS:
" Expected expression before '{' token"

I have tried reading a lot about this, but no success.



Since I am declaring rrc and rd as 1, all my 3 arrays will have one element only. So, I am taking the liberty to allocate only so much memory and put in a pre-determined value there.

I am on a Fedora 2.6.21-1. Using gcc compiler.
Really appreciate a solution to this.

Thanks!
TS
  #2  
Old 13-May-2008, 21:34
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Problem with arrays declared in a structure


Quote:
Originally Posted by tskd

I then try to allocate memory for these arrays and give them predetermined values.

CPP / C++ / C Code:
c_a_d->rrgn = {0x01};

c_a_d->rdi = {0x01};

c_a_d->rv = {0x03};
That {}notation can be used for an array initializer list, not in any kind of assignment statement.


For assignment statements. You can use pointer notation:
CPP / C++ / C Code:
    c_a_d->rrgn = malloc(sizeof(uint8_t) * c_a_d->rrc);
    *c_a_d->rrgn = 0x01;

    c_a_d->rdi = malloc(sizeof(uint8_t) * c_a_d->rd);
    *c_a_d->rdi = 0x01;

    c_a_d->rv = malloc(sizeof(uint8_t) * c_a_d->rd);
    *c_a_d->rv = 0x03;
Or you can use index notation
CPP / C++ / C Code:
    c_a_d->rrgn = malloc(sizeof(uint8_t) * c_a_d->rrc);
    c_a_d->rrgn[0] = 0x01;

    c_a_d->rdi = malloc(sizeof(uint8_t) * c_a_d->rd);
    c_a_d->rdi[0] = 0x01;

    c_a_d->rv = malloc(sizeof(uint8_t) * c_a_d->rd);
    c_a_d->rv[0] = 0x03;

Regards,

Dave
  #3  
Old 13-May-2008, 21:50
tskd tskd is offline
New Member
 
Join Date: May 2008
Posts: 3
tskd is on a distinguished road

Re: Problem with arrays declared in a structure


Thanks for the swift reply Dave.
I get the error.

Have a question based on your reply.
So suppose I had my rrc = 2 and rd = 2, how would I initialize my arrays?

*c_a_d->rr = 0x01;
*(c_a_d->rr + 1) = 0x04;

and so on...
Is this the only option? Could I initialize it more intelligently, like in a single statement, without using loops and stuff?
I cannot ask for user input, nor can I input this in a commandline.

Thanks,
TS
  #4  
Old 13-May-2008, 22:04
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Problem with arrays declared in a structure


Quote:
Originally Posted by tskd
Could I initialize...in a single statement, without using loops and stuff?
No. You can initialize declared arrays with a list in {} braces, but not dynamic "arrays" that use pointers and malloc().

Regards,

Dave
  #5  
Old 13-May-2008, 22:09
tskd tskd is offline
New Member
 
Join Date: May 2008
Posts: 3
tskd is on a distinguished road

Re: Problem with arrays declared in a structure


gotcha

thanks
 
 

Recent GIDBlogToyota - 2008 September Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem of c structure in .net ganesh_ghodke C Programming Language 7 26-Aug-2005 04:14
Problem with structure arrays BobTheHydra C Programming Language 7 12-Aug-2005 08:49
Structure compilation problem nkhambal C Programming Language 1 03-Aug-2004 08:16
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 15:13

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:06.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.