Define pointer. Discuss the relationship between pointer and one-dimensional array

Answers

This answer is not selected as best answer. This answer may not be sufficient for exam.

Your limit has been exceed. We have implemented this system because, We got difficulty on managing our servers. Please donate some amount to remove this limit.

Quota: 0 / 30

Donate

A pointer is a variable that contains a memory address of data or another variable. Normally, a pointer variable is declared to some type, like any other variables, so that it will work only with data of given type.

The syntax of pointer is

data_type *pointer;

Relationship between pointer and one-dimensional array

An array is a block of sequential data. Let’s write a program to print addresses of array elements.

#include <stdio.h>
int main() {
   int x[4];
   int i;

   for(i = 0; i < 4; ++i) {
      printf("&x[%d] = %p\n", i, &x[i]);
   }

   printf("Address of array x: %p", x);

   return 0;
}

The output of above program si

&x[0] = 1450734448
&x[1] = 1450734452
&x[2] = 1450734456
&x[3] = 1450734460
Address of array x: 1450734448

There is a difference of 4 bytes between two consecutive elements of array x. It is because the size of int is 4 bytes (on our compiler).

Notice that, the address of &x[0] and x is the same. It’s because the variable name x points to the first element of the array.

User Loaded Image | CSIT Guide

From the above example, it is clear that &x[0] is equivalent to x. And, x[0] is equivalent to *x.

Similarly,

  • &x[1] is equivalent to x+1 and x[1] is equivalent to *(x+1).
  • &x[2] is equivalent to x+2 and x[2] is equivalent to *(x+2).
  • Basically, &x[i] is equivalent to x+i and x[i] is equivalent to *(x+i).
If you found any type of error on the answer then please mention on the comment or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .