Arrays occupy consecutive memory slots in the computer's memory. This is where pointer arithmetic comes in handy - if you create a pointer to the first element, incrementing it one step will make it point to the next element. This will produce following result: See more examples on Pointers and Array Modifying Variables Using Pointers: This will produce following result: Generic Pointers: ( void Pointer ) NOTE-1 : Here in first print statement, the_data is prefixed by *(int*). This is called type casting in C language. Type is used to caste a variable from one data type to another datatype to make it compatible to the lvalue.#include
int main() {
int *ptr;
int arrayInts[10] = {1,2,3,4,5,6,7,8,9,10};
ptr = arrayInts; /* ptr = &arrayInts[0]; is also fine */
printf("The pointer is pointing to the first ");
printf("array element, which is %d.\n", *ptr);
printf("Let's increment it.....\n");
ptr++;
printf("Now it should point to the next element,");
printf(" which is %d.\n", *ptr);
printf("But suppose we point to the 3rd and 4th: %d %d.\n",
*(ptr+1),*(ptr+2));
ptr+=2;
printf("Now skip the next 4 to point to the 8th: %d.\n",
*(ptr+=4));
ptr--;
printf("Did I miss out my lucky number %d?!\n", *(ptr++));
printf("Back to the 8th it is then..... %d.\n", *ptr);
return 0;The pointer is pointing to the first array element, which is 1.
Let's increment it.....
Now it should point to the next element, which is 2.
But suppose we point to the 3rd and 4th: 3 4.
Now skip the next 4 to point to the 8th: 8.
Did I miss out my lucky number 7?!
Back to the 8th it is then..... 8.
You know how to access the value pointed to using the dereference operator, but you can also modify the content of variables. To achieve this, put the dereferenced pointer on the left of the assignment operator, as shown in this example, which uses an array:#include
int main() {
char *ptr;
char arrayChars[8] = {'F','r','i','e','n','d','s','\0'};
ptr = arrayChars;
printf("The array reads %s.\n", arrayChars);
printf("Let's change it..... ");
*ptr = 'f'; /* ptr points to the first element */
printf(" now it reads %s.\n", arrayChars);
printf("The 3rd character of the array is %c.\n",
*(ptr+=2));
printf("Let's change it again..... ");
*(ptr - 1) = ' ';
printf("Now it reads %s.\n", arrayChars);
return 0;The array reads Friends.
Let's change it..... now it reads friends.
The 3rd character of the array is i.
Let's change it again..... Now it reads f iends.
When a variable is declared as being a pointer to type void it is known as a generic pointer. Since you cannot have a variable of type void, the pointer will not point to any data and therefore cannot be dereferenced. It is still a pointer though, to use it you just have to cast it to another kind of pointer first. Hence the term Generic pointer. This is very useful when you want a pointer to point to data of different types at different times.
Try the following code to understand Generic Pointers.#include
int main()
int i;
char c;
void *the_data;
i = 6;
c = 'a';
the_data = &i;
printf("the_data points to the integer value %d\n",
*(int*) the_data);
the_data = &c;
printf("the_data now points to the character %c\n",
*(char*) the_data);
return 0;
}
NOTE-2 : lvalue is something which is used to left side of a statement and in which we can assign some value. A constant can't be an lvalue because we can not assign any value in contact. For example x = y, here x is lvalue and y is rvalue.
However, above example will produce following result:the_data points to the integer value 6
the_data now points to the character a
Using Pointer Arithmetic With Arrays:
Labels: C Learning