GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Dec-2007, 06:47
ajbharani's Avatar
ajbharani ajbharani is offline
New Member
 
Join Date: Dec 2007
Posts: 23
ajbharani is on a distinguished road

Create 2D Array!


hi

can we construct a 2Dimensional array with

int (*a)[10];

???????

thnx
  #2  
Old 08-Dec-2007, 07:35
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,688
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Create 2D Array!!!


Quote:
Originally Posted by ajbharani
can we construct a 2Dimensional array...

In the first place, in C (and C++) there actually isn't any such thing as a 2-Dimensional array. We talk about such things, and there is notation for such things, but actually there are arrays (1-Dimensional). You can have an array of arrays and call it a 2-Dimensional array, (and we usually do).

Your statement declares that a is a pointer to an array of 10 ints.
If we want an array of arrays of 10 ints, then we could do something like the following
CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n;

    int (*a)[10]; /* a pointer to an array of 10 ints */
    printf("sizeof(int)  = %d\n", sizeof(int));
    printf("sizeof(a[0]) = %d\n\n", sizeof(a[0]));

    printf("Enter a positive integer: ");
    if ((scanf("%d", &n) != 1) || (n < 1)) {
        printf("Invalid entry.\n");
        return 0;
    }
    printf("Allocating %d bytes for a.\n\n", n * sizeof(a[0]));
    a = malloc(n * sizeof(a[0]));
    if (a == NULL) {
        printf("malloc failed\n");
    }
    printf("Now you can access elements a[i][j] where\n");
    printf("  i is greater than or equal to zero and is less than %d\n", n);
    printf("  j is greater than or equal to zero and is less than 10\n\n");

    free(a);
    return 0;
}

Output:
Code:
sizeof(int) = 4 sizeof(a[0]) = 40 Enter a positive integer: 3 Allocating 120 bytes for a: Now you can access elements a[i][j] where i is greater than or equal to zero and is less than 3 j is greater than or equal to zero and is less than 10

Regards,

Dave
  #3  
Old 08-Dec-2007, 07:57
ajbharani's Avatar
ajbharani ajbharani is offline
New Member
 
Join Date: Dec 2007
Posts: 23
ajbharani is on a distinguished road

Re: Create 2D Array!!!


thnx..

wat is the diff in implementing a multi-dimensional array using
int *a[10] and int (*a)[10] ???

  #4  
Old 08-Dec-2007, 10:55
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,688
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Create 2D Array!!!


Quote:
Originally Posted by ajbharani
...the diff...


The following statement declares a to be an array of 10 pointers to int
CPP / C++ / C Code:
int *a[10];

a[0] is a pointer to int
a[1] is a pointer to int
.
.
.
a[9] is a pointer to int

You can access a particular integer in the array by a[i][j] where
i is greater than or equal to zero and less than 10
and
j is greater than or equal to zero and less than however many ints for which you allocate storage and assigned the memory to a[i]

A 2-D array from this would involve allocating storage and assigning an address for each of the a[i]. You could consider that the array has 10 "rows," and each row can have as many ints as you allocate.

In the previous example, you end up with an array that has as many "rows" as you want (the value of n in the program), and each row has 10 ints.

So in the previous example
a[0] is a pointer to an array of 10 ints
a[1] is a pointer to an array of 10 ints.
.
.
a[n-1] is a pointer to an array of 10 ints.

You can access any integer by a[i][j] where
i is greater than or equal to zero and less than n
j is greater than or equal to zero and less than 10


Regards,

Dave
  #5  
Old 09-Dec-2007, 20:28
ajbharani's Avatar
ajbharani ajbharani is offline
New Member
 
Join Date: Dec 2007
Posts: 23
ajbharani is on a distinguished road

Re: Create 2D Array!!!


nice piece of info . .
thnx
 
 

Recent GIDBlogToyota - 2008 August Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How Do I Create a Function that returns a usable array of pointers to structures? UncleRic C Programming Language 2 03-Sep-2007 11:17
How to sort in C++ alphabetically wilen C++ Forum 5 20-Apr-2007 14:43
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 19:31
Pointer Usage in C++: Beginner to Advanced varunhome C++ Forum 0 19-Aug-2005 09:25
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 21:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:57.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.