| Author |
Problem with dynamic array in C?
|
Jack Drowder
Greenhorn
Joined: Oct 01, 2012
Posts: 2
|
|
So I was testing out dynamic arrays in C, and I created this incredibly simple program:
The output is:
Why doesn't it print 1? Am I missing something?
Thank You!
|
 |
ahmed hamdy
Greenhorn
Joined: Dec 31, 2012
Posts: 2
|
|
you did not allocate any space
you must declare like that:
datatype* variable =(datatype*)mallco(size*sizeof(datatype));
|
 |
ahmed hamdy
Greenhorn
Joined: Dec 31, 2012
Posts: 2
|
|
your output may be rabbitch from memory
i had test it and output was 2
|
 |
Ivan Jozsef Balazs
Ranch Hand
Joined: May 22, 2012
Posts: 380
|
|
You printed the size of a pointer (happening to be pointing to character - but this is irrelevant).
Obviously the size does not depend on the contents of the memory area being pointed to. The size's equaling 4 should not be a big surprise on a 32 bit platform.
|
 |
Ivan Jozsef Balazs
Ranch Hand
Joined: May 22, 2012
Posts: 380
|
|
This would print 1
|
 |
 |
|
|
subject: Problem with dynamic array in C?
|
|
|