/******* * example of arrays and pointer arithmetic in C * * $ make arrays * gcc -march=athlon64 -O2 arrays.c -o arrays * * $ ./arrays * address of array1 is '0x804a008'. * sizeof(int) is '4'. * address of array1[0] is '0x804a008'. * address of array1[1] is '0x804a00c'. * address of (int)array1 + 1*sizeof(int) is also '0x804a00c'. * ... but address of array1 + sizeof(int) is '0x804a018' ?? * * 1st, 2nd values of array1 are '0' and '10'. * Same from explicit pointers are '0' and '10'. * * String1 is 'Hi there.'. * The first character of string2 is 'H'. * * $Id: arrays.c 9938 2007-01-29 20:51:37Z mahoney $ ********/ #include #include #include // Both of these declarations are the same: a pointer to a character, // i.e.a string. Neither allocates space. void string_play(char *str1, char str2[]){ // Strings in C are pointers to arrays of characters. // The end of the string is marked by a '0' byte which isn't usually // the last allocated byte of memory for the string to sit it. // Pretty much anything you want to do must be done with subroutine calls. // For example, to copy one string to another : strcpy(str1, str2); // In python this would just be " string1 = string2 " // In formatted strings, "%s" is a string, // which means its variable should be a char*. printf("\n String1 is '%s'.\n", str1); // On the other hand, "%c" is a character, which means its variable should be a char. printf(" The first character of string2 is '%c'.\n", *str2); } // ================================================================= // int main(){ int *array1; // a pointer to an integer ... which is an array ! int* array2; // same; the asterisk can go in either place int array4[100]; // same but also allocates space for the array. int i; char string1[256]; // allocates memory for string of 256 characters. char string2[] = "Hi there."; // shortcut; allocates & fills memory from given string. // Allocate space for array1 explicitly. // = () memory_alloate() array1 = (int*) malloc(100*sizeof(int)); // Put some values in array1. for (i=0; i<10; i++){ array1[i] = 10*i; } printf("address of array1 is '%p'.\n", array1); printf("sizeof(int) is '%i'.\n", sizeof(int)); printf("address of array1[0] is '%p'.\n", &(array1[0])); printf("address of array1[1] is '%p'.\n", &(array1[1])); printf("address of (int)array1 + 1*sizeof(int) is also '%p'.\n", (int)array1 + 1*sizeof(int)); printf("\n1st, 2nd values of array1 are '%i' and '%i'.\n", array1[0], array1[1]); printf("Same with C pointer arithmetic: '%i' and '%i'.\n", *(array1), *(array1+1)); printf("Same with explicit types : '%i' and '%i'.\n", *array1, *((int*)((int)array1 + 1*sizeof(int)))); // ---------------------------------------------------------------------- // Notice that adding a number to a pointer is doing something tricky in C : // the compiler notices what sort of thing is being pointed at, and increments // the pointer by that many bytes each time you add "1". // This means that you should *not* add "sizeof" anything to a pointer. // printf("This does the wrong thing: address of array1 + sizeof(int) is '%p' \n", // array1 + sizeof(int)); // ---------------------------------------------------------------------- // The upshot of all this is that in C, an "array" is really just a pointer, // and array[n] really refers to *(array + n*sizeof()) // which is literally the address of the n'th thing in the array. // Finally, here's a bit of string stuff for comparison. string_play(string1, string2); }