By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,920 Members | 1,660 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,920 IT Pros & Developers. It's quick & easy.

[Q] Initialise a struct with variable length cahracter strings

P: n/a
Dear Readers,

I am attempting to initialise a struct contiaing a dynamic character
string. In the example below I am trying to initialise the name field
so that my struct does not waste space. I know if I change char
name[80] this will work, but I will waste alot of space. (I am
learning so I want to learn the best way)

How can I define a struct the allows variable length character strings
in the
definition?

Thank

Stuart

#include <stdio>
#include <stdlib>

int main (int argc, char **argv)
{
struct command
{
unsigned int cmd;
unsigned int olen;
unsigned int ilen;
char name[];
} init[] = {{0,0,0,"Name A"},{0,0,0,"Name B 123"}};
}
Nov 13 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Stuart Norris wrote:
How can I define a struct the allows variable length character strings
in the
definition? struct command
{
unsigned int cmd;
unsigned int olen;
unsigned int ilen;
char name[]; char *name; } init[] = {{0,0,0,"Name A"},{0,0,0,"Name B 123"}};


--
Tom Zych
This is a fake email address to thwart spammers.
Real address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #2

P: n/a

"Stuart Norris" <st**********@yahoo.com.au> wrote in message
news:51**************************@posting.google.c om...
Dear Readers,

I am attempting to initialise a struct contiaing a dynamic character
string. In the example below I am trying to initialise the name field
so that my struct does not waste space. I know if I change char
name[80] this will work, but I will waste alot of space. (I am
learning so I want to learn the best way)

How can I define a struct the allows variable length character strings
in the
definition?

Thank

Stuart

#include <stdio>
#include <stdlib>

int main (int argc, char **argv)
{
struct command
{
unsigned int cmd;
unsigned int olen;
unsigned int ilen;
char name[];
const char *name;
} init[] = {{0,0,0,"Name A"},{0,0,0,"Name B 123"}};
}


Note that you must not modify what 'name' points to.

-Mike
Nov 13 '05 #3

P: n/a
On 24 Sep 2003 17:53:35 -0700, st**********@yahoo.com.au (Stuart
Norris) wrote:
Dear Readers,

I am attempting to initialise a struct contiaing a dynamic character
string. In the example below I am trying to initialise the name field
so that my struct does not waste space. I know if I change char
name[80] this will work, but I will waste alot of space. (I am
learning so I want to learn the best way)

How can I define a struct the allows variable length character strings
in the
definition?

Thank

Stuart

#include <stdio>
#include <stdlib>
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char **argv)
{
struct command
{
unsigned int cmd;
unsigned int olen;
unsigned int ilen;
char name[];
This is an incomplete type which means that the entire structure
will also be an incomplete type.

You cannot define an array of incomplete types. Change this
to:
char *name;
or:
char name[11];
} init[] = {{0,0,0,"Name A"},{0,0,0,"Name B 123"}};
}


Nick.

Nov 13 '05 #4

P: n/a


Stuart Norris wrote:
Dear Readers,

I am attempting to initialise a struct contiaing a dynamic character
string. In the example below I am trying to initialise the name field
so that my struct does not waste space. I know if I change char
name[80] this will work, but I will waste alot of space. (I am
learning so I want to learn the best way)

How can I define a struct the allows variable length character strings
in the
definition?
struct command
{
unsigned int cmd;
unsigned int olen;
unsigned int ilen;
char name[];
} init[] = {{0,0,0,"Name A"},{0,0,0,"Name B 123"}};


It it is unacceptable to declare fixed arrays then I would suggest
that you dynamically allocated space. Write a function that will
allocate and assign.

#include<stdio.h>
#include<stdlib.h>
#include <string.h>

struct command
{
unsigned int cmd;
unsigned int olen;
unsigned int ilen;
char *name;
};

struct CommandArray
{
struct command *command;
unsigned count;
};

struct command *AddStruct(struct CommandArray *p, unsigned cmd,
unsigned olen, unsigned ilen, const char *name);
void FreeStruct(struct CommandArray *p);

int main(void)
{
struct CommandArray test = {NULL, 0};
unsigned i;

AddStruct(&test, 0,0,0, "Name A");
AddStruct(&test, 0,0,0, "Name B 123");
for(i = 0; i < test.count; i++)
printf("test.command[%u].name = \"%s\"\n",
i,test.command[i].name);
FreeStruct(&test);
return 0;
}

struct command *AddStruct(struct CommandArray *p, unsigned cmd,
unsigned olen, unsigned ilen, const char *name)
{
struct command *temp;
unsigned i = p->count;

temp = realloc(p->command, (sizeof *p->command)*(i+1));
if(temp == NULL) return NULL;
p->command = temp;
if((p->command[i].name = malloc(strlen(name)+1)) == NULL)
return NULL;
strcpy(p->command[i].name,name);
p->command[i].ilen = ilen;
p->command[i].olen = olen;
p->command[i].cmd = cmd;
p->count++;
return &p->command[i];
}

void FreeStruct(struct CommandArray *p)
{
unsigned i;

for(i = 0;i < p->count; i++)
free(p->command[i].name);
free(p->command);
p->command = NULL;
p->count = 0;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa*@abowers.combase.com (remove the x)
http://www.geocities.com/abowers822/

Nov 13 '05 #5

P: n/a
On Thu, 25 Sep 2003 04:39:42 +0100, Nick Austin
<ni**********@nildram.co.uk> wrote:
On 24 Sep 2003 17:53:35 -0700, st**********@yahoo.com.au (Stuart
Norris) wrote: <snip>
struct command
{
unsigned int cmd;
unsigned int olen;
unsigned int ilen;
char name[];


This is an incomplete type which means that the entire structure
will also be an incomplete type.

In C90, that is a constraint violation; a struct (or union) member
cannot have incomplete type at all. In C99, as the last member, that
is a flexible array member, the blessed form of the struct hack. It
does not make the containing struct incomplete; instead the struct
type has only the size of the fixed header, and you the programmer are
responsible for allocating/managing memory space for any objects that
also contain the variable part.
You cannot define an array of incomplete types.
True, but irrelevant in either case. You *can* have an array of
FAMiful elements, but they do not allow any space for the variable
parts, as is needed in this application.
Change this
to:
char *name;
or:
char name[11];
} init[] = {{0,0,0,"Name A"},{0,0,0,"Name B 123"}};
}

Right. Of course the former wastes space, which is what the OP asked
to avoid, although unless there are thousands of these or the maximum
length is (and needs to be) much more than the 11 shown or this is a
very constrained embedded system I wouldn't worry about it; and the
latter wastes a pointer in each struct as well as making the targets
not (safely) writable, if that matters.

The FAMly alternative, which cannot be initialized in the language
sense, but only set at runtime perhaps startup:
struct command * array[WHATEVER];
/* error checking omitted */
array[0] = malloc(sizeof(struct command)+strlen(str1)+1);
array[0]->cmd = 0; ... strcpy(array[0]->name, str1);
array[1] = malloc(sizeof(struct command)+strlen(str2)+1);
array[1]->cmd = 0; ... strcpy(array[1]->name, str2);
etc.
- David.Thompson1 at worldnet.att.net
Nov 13 '05 #6

P: 2
On Thu, 25 Sep 2003 04:39:42 +0100, Nick Austin
<nickDIGITONE@nildram.co.uk> wrote:
[color=blue]
> On 24 Sep 2003 17:53:35 -0700, stuie_norris@yahoo.com.au (Stuart
> Norris) wrote:[/color]
<snip>[color=blue][color=green]
> > struct command
> > {
> > unsigned int cmd;
> > unsigned int olen;
> > unsigned int ilen;
> > char name[];[/color]
>
> This is an incomplete type which means that the entire structure
> will also be an incomplete type.
>[/color]
In C90, that is a constraint violation; a struct (or union) member
cannot have incomplete type at all. In C99, as the last member, that
is a flexible array member, the blessed form of the struct hack. It
does not make the containing struct incomplete; instead the struct
type has only the size of the fixed header, and you the programmer are
responsible for allocating/managing memory space for any objects that
also contain the variable part.
[color=blue]
> You cannot define an array of incomplete types.[/color]

True, but irrelevant in either case.
- David.Thompson1 at worldnet.att.net
Sorry for gravedigging, but this post was relevant to a question I have.

I need to copy a file into a buffer that is part of a struct, then pass a reference to the struct into a callback function (for an audio API). So, could I use this code (abridged):

Expand|Select|Wrap|Line Numbers
  1. typedef struct
  2. {
  3.     int channels;
  4.     double samplerate;
  5.     unsigned double framesToGo;
  6.     char *buffer;
  7. }
  8. PlaybackData;
  9.  
  10. int main()
  11. {
    int filesize; /* this would be the size of the file once loaded*/
  12. PlaybackData data;
  13. data.buffer = malloc( filesize );
  14. }
  15.  
Would I also need to malloc for the entire struct, so the actual pointer would have some space?

Thanks.
Jun 17 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.