• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with dynamic array in C?

 
Jack Drowder
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you did not allocate any space

you must declare like that:

datatype* variable =(datatype*)mallco(size*sizeof(datatype));

 
ahmed hamdy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your output may be rabbitch from memory

i had test it and output was 2
 
Ivan Jozsef Balazs
Rancher
Posts: 972
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 972
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This would print 1
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic