Results 1 to 2 of 2

Thread: Arrays and Pointers in C

  1. #1
    Join Date
    May 2008
    Posts
    18

    Arrays and Pointers in C

    Your computer memory is divided into a bunch of “boxes” and each “box” has a size of 1 byte. You must know about, at least, the basic model of memory in order to use or understand pointers. Let’s first look at a simple variable.

    int x;

    In the above line, we are declaring a variable called x which is of an integer type. A computer must somehow store this variable in order to use it in the future. So it stores this variable in the memory. But where? This type of simple variable is stored somewhere in your program’s memory space. An integer has a size of 4 bytes (in old compilers it is 2 bytes), so it’ll occupy 4 “boxes” of your memory. Nothing else can use this memory space because it holds your variable unless your program exists. Yes! You can’t “empty” this memory space after you have put your variable x in there. A memory model containing x would like this:

    | | | | |

    Right now there’s nothing in those 4 “boxes” but as you assign some number to x, some or all of these boxes will be filled up according to the size of your number.

    x = 4;

    Now you have assigned a value of 4 to x. You can retrieve the value by writing x like this:

    printf("%d",x);

    So we can conclude that a value is some sort of data that is held in the memory space reserved for your variable.

    Above theory shows you how variable contains a value and how it’s organized in the memory. Now let's talk about a simple array.

    int x[4];

    The above line declares an array of 4 integer. How much memory do you think is reserved for this variable (for now)? It’s not 4 bytes but 16 bytes!! Now at the same time Let's start talking about pointers...

    A pointer is a variable, as usual, but it’s VALUE is an address of some other variable. You declare a pointer with prefix of * like this:

    int *p;

    Now if we want to access the value of p, we simply use the name “p”. But the pointer’s value will holds some memory address. It is usually the address of another variable but not always!

    The above code will print a hexadecimal address of x. See! The actual value of our pointer p holds an address. Now try this:

    int x = 3; //Declare a variable
    int *p; //Declare a pointer
    p = &x; //p now holds the memory address of x
    printf("%d",*p);

    You use the “&” symbol to access the memory address of a variable - NOT THE VALUE! The above statement will print the actual value of x. But we didn’t really use the variable x to get the value. Because p already holds the address of x, you can use the prefix * to retrieve whatever is on the address that p holds. When we used ,*p, it actually looks at the value of p. The value of p is actually an address of some place in the memory so *p returns the value on that address.

    A pointer has 3 “things” compared to a variable which only has 2 “things”. A variable has a value and an address while a pointer has a value (address of some variable), its own memory address (we can get it using &p), and a pointed value which it gets by looking at its value (again, the memory address of another variable). We get the value using *p as we wrote above.

    Let’s declare an array now:

    int x[4];

    It’s 16 bytes long in memory. The start of this array is x[0] so we can get the starting address of this array by using &x[0] (remember you use the “&” sign to get the address of a variable). Now remember this equation for an array (x is an array in this example):

    x = &x[0];

    So if we have an array x, we can simply use x to retrieve the starting address of the array in the memory. We can now simply declare a pointer that points to the start of the array thus the value of our pointer will be the starting address of the array in the memory.

    int x[4] = {1,2,3,4};
    int *p;
    p = x;

  2. #2
    Join Date
    May 2008
    Posts
    18

    Re: Arrays and Pointers in C

    We are just assigning an address to p which is completely legal. Now try outputting the value of *p. You’ll see “1" which is exactly the value of first item in our array! You can also increase the value of the pointer so that it points to next integer in the array. It’ll simply look at the type of the pointer. Because we declared it as an integer, it’ll increase the value of the pointer by 4 bytes (it’ll add 4 bytes to the memory address). Let’s try it:

    #include <stdio.h>

    void main()

    {

    int x[4] = {1,2,3,4};

    int *p = x;

    int i;

    for(i=0;i<4;i++)

    {

    printf("\n%d",*p);

    p++;

    }

    }

    The output will look this:

    1

    2

    3

    4

Similar Threads

  1. What are the advantages of pointers?
    By Owen Fernandes in forum Software Development
    Replies: 5
    Last Post: 09-02-2010, 12:39 PM
  2. Pointers in C and C++
    By unlimitedtech in forum Guides & Tutorials
    Replies: 4
    Last Post: 04-02-2010, 12:42 PM
  3. Pointers VS Arrays in C++
    By Rum in forum Software Development
    Replies: 5
    Last Post: 18-01-2010, 02:06 PM
  4. Pointer to an array of pointers
    By pushpendra in forum Software Development
    Replies: 3
    Last Post: 29-12-2009, 12:45 PM
  5. Are pointers and arrays equivalent
    By KALINDA in forum Software Development
    Replies: 3
    Last Post: 16-10-2009, 06:41 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,271,788.00194 seconds with 17 queries