Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home C-tutorials C array of pointers

Related Tutorials


 
 

Share on Google+Share on Google+

C array of pointers

Advertisement
A pointer is a variable that contains the memory location of another variable.

C array of pointers

     

In this section, you will learn how to create array of pointers.

A pointer is a variable that contains the memory location of another variable. The values you assign to the pointers are memory addresses of other variables (or other pointers). A running program gets a certain space in the main memory. 

Syntax of declaring a pointer:

  data_type_name * variable name


First of all, specify the data type of data stored in the location, which is to identified by the pointer. The asterisk tells the compiler that you are creating a pointer variable. Then specify the name of variable.

You can see in the given example, we have created an array of pointers of maximum size 3. Then we have assigned the objects of array pointer.
The address of the variable. 
array[0] = &x;
array[1] = &y;
array[2] = &z;

Here is the code:

ARRAYPOI.C

#include <stdio.h>
#include <conio.h>
main() {
  clrscr();
  int *array[3];
  int x = 10, y = 20, z = 30;
  int i;
  array[0= &x;
  array[1= &y;
  array[2= &z;
  for (i=0; i< 3; i++) {
  printf("The value of %d= %d ,address is %u\t \n", i, *(array[i]),
  array[i]);
  }
  getch();
  return 0;
}

Output will be displayed as:

ARRAYPOI.EXE

Download Source Code:

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 5, 2009

Related Tutorials

Discuss: C array of pointers  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:6
sri
November 16, 2011
array

very clear notes
kumar
February 8, 2012
hi

plz send clear explanation of following program: main() { char c[]="gate2011"; char *p=c; printf("%s",p+p[3]-p[1]); }
sanjeev kumar
May 19, 2012
c++

its very good subject
shan
June 15, 2012
thanks

thanks for post this topic its very useful :D
Ben
November 17, 2012
Array of Pointers

If I have an array of pointers as you defined above: ie. float *float_ptr[4] and I wanted to do the following: store the data set [0 1 2 3 4 5] into float_ptr[0] and store [0 1 3 4 8 10] into float_ptr[1] similarly for the next two, how would I go about doing that? I have an idea as listed below, however the syntax doesn't yield the results I want, obviously which is why I am writing you. int j, i; int array1[24] = {0, 1, 2, 3, 4, 5 , 0, 1, 3, 4, 8, 10, 1, 9, 3, 4, 6, 21, 0, 1, 1, 9, 6, 12}; float *float_ptr[4], a, b, c, d; for (j = 0; j < 4; j++) { for (i = 0; i < n; i++) { if (i == 0) { a = (float)array1[i * 4 + j] - 1.88; float_ptr[chan_int][i * 4 + j] = a; } else if (i == 1) { b = (float)array1[i * 4 + j] - 1.88; float_ptr[chan_int][i * 4 + j] = b; } else if (i == 2) { c = (float)array1[i] - 1.88; float_ptr[chan_int][i * 4 + j] = c; } else if (i == 3) { d = (float)array1[i] - 1.88; float_ptr[chan_int][i * 4 + j] = d; } } } As you can see there is a double index for float_ptr, which is the issue, and I am not sure what to replace this with. Thanks!! -Ben
suhasini
February 21, 2013
c,c++

is there any need to learn these language? ........kindly plz tell me
DMCA.com