Thread: 2D arrays

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    1

    2D arrays

    Hi ,
    I am new to pointers and I have a small understanding problem here.

    If I declare int **c does this mean
    1. I am declaring a dynamic 2D integer array

    int **c
    c = malloc(size_x * sizeof(int *));
    for (i = 0; i < size_x; i++)
    c[i] = malloc(size_y * sizeof(int));


    Or

    2. I am declaring an integer pointer which holds the address of another integer pointer

    int a;
    int *b;
    int **c;
    b =&a;
    c=&b; /* here C holds the address of the pointer b*/


    My question here is what does int **c mean when the above 2 scenarios are taken into consideration.
    Thanks in advance for the help

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Neither. It means that c is a pointer to a pointer to an int. You can then use it to point to the first pointer in a dynamic array of pointers where each pointer points to the first character of an array of int, and thus simulate a dynamic 2D array, or you can use it to point to a lone pointer to int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  2. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  3. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  4. Parallel Arrays with Multiple Arrays
    By Billye Scott in forum C++ Programming
    Replies: 0
    Last Post: 03-02-2002, 11:14 PM
  5. separating line of arrays into array of arrays
    By robocop in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2001, 12:43 AM