aspose file tools*
The moose likes C / C++ and the fly likes Problem with dynamic array in C? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » C / C++
Bookmark "Problem with dynamic array in C?" Watch "Problem with dynamic array in C?" New topic
Author

Problem with dynamic array in C?

Jack Drowder
Greenhorn

Joined: Oct 01, 2012
Posts: 7
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
Rancher

Joined: May 22, 2012
Posts: 908
    
    5
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
Rancher

Joined: May 22, 2012
Posts: 908
    
    5
This would print 1
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with dynamic array in C?