dynamically allocate array size using malloc

S

SAUHING LEE

I am a beginner in C. I want to dynamically allocate the size of two
arrays inside a struct at compile time.(the size of the array is not
known before user input it).
The following code won't compile because of not specifying the array
size.
Any know how to do it using malloc?



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

typedef struct {
int list[];
int array[];

} array_holder;


int main()
{
int array_size;
scanf(" %d", &array_size);

}
 
V

vippstar

I am a beginner in C. I want to dynamically allocate the size of two
arrays inside a struct at compile time.(the size of the array is not
known before user input it).
The following code won't compile because of not specifying the array
size.
Any know how to do it using malloc?

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

typedef struct {
int list[];
int array[];

That's not valid. You can't have members of a struct with incomplete
type. Maybe you meant int *list, *array;
} array_holder;

int main()
{
int array_size;
scanf(" %d", &array_size);

Well, that's clearly incomplete.

It seems your code is homework. I'll only give you hints:

1) `d' reads a signed int, and may read a negative value. Negative
values passed to malloc, realloc, calloc will be really big values.
2) there's no reason for your format string to be " %d". %d will read/
skip characters of the space class at start.
3) Check the return value of scanf. It doesn't always succeed. See its
manual page or a good reference to find out how.
4) list and array should be int *. The number entered by the user
should be passed to malloc, and the return value of the function
stored to each pointer for each call. Don't forget to check if malloc
succeeded, consult your reference/man pages to find out how.
 
R

Richard Tobin

SAUHING LEE said:
The following code won't compile because of not specifying the array
size.
typedef struct {
int list[];
int array[];

} array_holder;

You can't do that. You can do it for the last array (by declaring it
as you do, in C99, or by declaring it to have 1 element but
malloc()ing space for more, in earlier versions), but not for others;
the offset of a field in a struct must be constant.

The simplest solution is to replace your arrays with pointers, and
separately malloc() space for them to point to.

-- Richard
 
B

Ben Bacarisse

SAUHING LEE said:
I am a beginner in C. I want to dynamically allocate the size of two
arrays inside a struct at compile time.
typedef struct {
int list[];
int array[];
} array_holder;

Another thing to consider is that, since both are arrays of the same
type, you don't need two. You could just have one and indicate where
the split between the two is. An obvious way to indicate this is with
a pointer into the one big array.

Once you have only one array whose size you don't know, you can use a
feature of modern C to represent your data:

struct data {
int *list;
int array[];
};

struct data *dp =
malloc(sizeof *dp + (list_sz + array_sz) * sizeof dp->array[0]);
if (dp) {
dp->list = &dp->array[array_sz];
...
}

In older C you can do this in a slightly dubious, round-about way --
search for the struct hack.
 
K

Keith Thompson

Ben Bacarisse said:
In older C you can do this in a slightly dubious, round-about way --
search for the struct hack.

A good source of information on the struct hack is question 2.6 of the
comp.lang.c FAQ, <http://www.c-faq.com/>. Since the FAQ doesn't use
the phrase "struct hack" (except in the URL), a web search is likely
not to find it.
 
P

Phil Carmody

I'm surprised no-one's jumped on the "dynamically" ... "at compile time"
part. "Dynamically" implies at run time.
typedef struct {
int list[];
int array[];
} array_holder;

Another thing to consider is that, since both are arrays of the same
type, you don't need two. You could just have one and indicate where
the split between the two is.

Depending on circumstances, and, given that OP said "the size of" rather
than "the sizes of", I think this might be appropriate, one could simply
interleave the two arrays such that one has ``int both[][2];''. Of
course, you're then left with an otherwise empty struct, which C won't
let you do. (However, you might well want to store the size of the
array in that structure, which would make it non-empty.)

Phil
 
G

Guest

I am a beginner in C. I want to dynamically allocate the size of two
arrays inside a struct at compile time.(the size of the array is not
known before user input it).
The following code won't compile because of not specifying the array
size.
Any know how to do it using malloc?
#include <stdio.h>
#include <stdlib.h>
typedef struct  {
                     int list[];
                     int array[];

That's not valid. You can't have members of a struct with incomplete
type. Maybe you meant int *list, *array;
                      } array_holder;
int main()
{
       int array_size;
       scanf(" %d", &array_size);

Well, that's clearly incomplete.

It seems your code is homework. I'll only give you hints:

it doesn't feel like "homework" to me
1) `d' reads a signed int, and may read a negative value. Negative
values passed to malloc, realloc, calloc will be really big values.
2) there's no reason for your format string to be " %d". %d will read/
skip characters of the space class at start.

um, isn't that what he wants
 
V

vippstar

On Dec 5, 10:53 pm, (e-mail address removed) wrote:


it doesn't feel like "homework" to me

Why? It was just a guess anyway
um, isn't that what he wants

Look closely: His format string is " %d" (note the space). The space
there tells scanf to read/skip all characters belonging in the space
class (isspace). The behavior of " %d" and "%d" alone is the same.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top