58
NotMe
7y

My teacher told us, that the array size in C is useless, becouse the array is dynamic and can be bigger or smaller everytime you want.. Happy overriding..

Comments
  • 8
    Oh jeez
  • 0
    I didnt use c for a few years but aren't arrays just pointers so you can realloc their size at any time as needed right?
  • 2
    Run Forrest run
  • 5
    @spacem Yeah. Arrays are pointers to the first memory location of a contiguous block.
    You can resize them by reallocating a bigger block and copying elements over, just like vector in cpp or ArrayList in java does when you add and remove items
  • 6
    @JonnyCodewalker that's a beautiful feauture indeed, doesn't lead to problems at all
  • 2
    @JonnyCodewalker true true, I killed myself so many times I must be immortal
  • 1
    @JonnyCodewalker sI much true, so much pain 😀
  • 0
    @Lasagna I didn't mean the realloc function when i said reallocating. What i meant is something like this
    1.get number of items in original array: sizof(arr)/sizeof(type)
    2.Create a new block with malloc: malloc(size * 2)
    3.copy all elements to new block.
    4.Append new items at the end.
    When the block is almost full, do the same thing.
    not using realloc ()
  • 0
    @Lasagna K&R page 83
    " ..the name of an array is a synonym for the location of the initial element.."
    In fact K&R treat them as pointers to the first element and can increment or decrement them to access the array elements
  • 1
    Try this:

    char arr[100];
    sizeof(arr); // 100
    sizeof(&arr[0]); // 8 on 64 bit

    Arrays are not pointers, but they are implicitly coerced to pointers in a lot of places (e.g. Function calls)
  • 0
    Just wait until university, dude. I promise it gets better.
  • 0
    @Lasagna Mmh.Interesting.
  • 0
    @awnumar That was in university..
Add Comment