Quick Navigation Bar pointers :: arrays :: dynamic memory allocation [ toc | forums ] |
Note: If the document URL does not begin with https://randu.org/tutorials/c/ then you are viewing a copy. Please direct your browser to the correct location for the most recent version. |
int ia[5];This would effectively make an area in memory (if availble) for ia, which is
5 * sizeof(int)
. We will discuss
sizeof()
in detail in Dynamic Memory Allocation.
Basically sizeof()
returns the size of what is
being passed. On a typical 32-bit machine, sizeof(int)
returns 4 bytes, so we would get a total of 20 bytes of memory
for our array.[ ]
we can effectively "dereference"
those areas of the array to return values.
printf("%d ", ia[3]);This would print the fourth element in the array to the screen. Why the fourth? This is because array elements are numbered from 0.
int x = 5; int ia[x];This above example is illegal. ANSI C restricts the array intialization size to be constant. So is this legal?
int ia[];No. The array size is not known at compile time.
#define MAX_ARRAY_SIZE 5 /* .... code .... */ int ia[MAX_ARRAY_SIZE];Now if we wanted to change the array size, all we'd have to do is change the define statement!
int ia[5] = {0, 1, 3, 4}; int ia[ ] = {0, 2, 1};Both of these work. The first one, ia is 20 bytes long with 16 bytes initialized to 0, 1, 3, 4. The second one is also valid, 12 bytes initialized to 0, 2, 1. (Examples on a typical 32-bit machine).
int ia[6] = {0, 1, 2, 3, 4, 5}; /* 1 */ int *ip; /* 2 */ ip = ia; /* equivalent to ip = &ia[0]; */ /* 3 */ ip[3] = 32; /* equivalent to ia[3] = 32; */ /* 4 */ ip++; /* ip now points to ia[1] */ /* 5 */ printf("%d ", *ip); /* prints 1 to the screen */ /* 6 */ ip[3] = 52; /* equivalent to ia[4] = 52 */ /* 7 */Ok, so what's happening here? Let's break this down one line at a time. Refer to the line numbers on the side:
sizeof()
.int igrid[2][3] = { {0, 1, 2}, {3, 4, 5} }; int igrid[2][3] = { 0, 1, 2, 3, 4, 5 }; int igrid[ ][4] = { {0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9} }; int igrid[ ][2];The first three examples are valid, the last one is not. As you can see from the first two examples, the braces are optional. The third example shows that the number of rows does not have to be specified in an array initialization.
getopt
. Remember the variable argv
?
It can be declared in the main function as either **argv
or *argv[]
. What does **argv
mean? It looks
like we have two pointers or something. This is actually a pointer
to a pointer. The *argv[]
means the same thing, right?
Imagine (pardon the crappy graphics skills):
argv +---+ | 0 | ---> "./junk" +---+ | 1 | ---> "-b" +---+ | 2 | ---> "gradient" +---+ | 3 | ---> "yeehaw" +---+So what would argv[0][1] be? It would be the character '/'. Why is this? It's because strings are just an array of characters. So in effect, we have a pointer to the actual argv array and a pointer at each argv location to each string. A pointer to a pointer. We will go more in depth into strings later.
int ia[2] = {0, 1}; printf("%d ", ia[2]);The above code would segfault, because you are trying to look at an area of memory not inside the array memory allocation.
void func(int ia[]) void func(int *ia)Both are the same declaration (you should know why by now). But why would this cause problems? Because only the pointer to the array is passed in, not the whole array. So what if you mistakenly did a
sizeof(ia)
inside func? Instead of
returning the sizeof the whole array, it would return the size of
a pointer which corresponds to the word size of the computer.
Notice: Please do not replicate or copy these pages and
host them elsewhere. This is to ensure that the latest version can always
be found here.
Disclaimer: The document author has published these pages
with the hope that it may be useful to others. However, the document
author does not guarantee that all information contained on these
webpages are correct or accurate. There is no warranty, expressed or
implied, of merchantability or fitness for any purpose. The author does
not assume any liability or responsibility for the use of the information
contained on these webpages.
If you see an error, please send an email to the address below indicating
the error. Your feedback is greatly appreciated and will help to
continually improve these pages.