Author Topic: pointers and 2d array  (Read 706 times)

0 Members and 1 Guest are viewing this topic.

Offline hs1993

  • NULL
  • Posts: 3
  • Cookies: 0
    • View Profile
pointers and 2d array
« on: February 07, 2016, 09:19:28 pm »
hi .. im trying to understand pointers more :

int x[2][5] = {{5,8,9,10,11},{12,13,12,33,13}};
int (*p)[5] = x;

printf("%d\t\%d\n",sizeof(p),sizeof(x));

---------------------------------------------------
output :
4       40

so my first question :
int (*p)[5] = x; << is this array of pointers ?

if not why size of p is 4 only ?

-------------------------------------------------


Offline ca0s

  • VIP
  • Sir
  • *
  • Posts: 432
  • Cookies: 53
    • View Profile
    • ka0labs #
Re: pointers and 2d array
« Reply #1 on: February 07, 2016, 10:27:49 pm »
Code: [Select]
http://c-faq.com/aryptr/ptrary2.html

So:

Code: [Select]
int (*p)[5]

is declaring a pointer to an array, and, as a pointer in 32bit wold, its size is 4 bytes. The thing that probably confused you is:

Code: [Select]
int *p[5];

which is an array of pointers, whose size would be 40 bytes.

It is more clearly explained at:
Code: [Select]
http://c-faq.com/aryptr/ptrtoarray.html
« Last Edit: February 07, 2016, 10:44:26 pm by ca0s »

Offline hs1993

  • NULL
  • Posts: 3
  • Cookies: 0
    • View Profile
Re: pointers and 2d array
« Reply #2 on: February 08, 2016, 10:22:14 am »
thanks ,, thats very help ful .. is there a way to make a pointer to (unknown size of 2d array) ?

i mean :

if i want to pass 2D array to a function .. and that function should work on all 2d arrays ... how is that possible .. i mean i dont know the array size at compilation time .. not after of course ..

im thinking of pointer to pointer .. not pointer to array .. but im actually new in both (classes) and (pointers) .. and i dont want to make mistake where i make (array of pointers) in that class which would take too much resources when the class is created ..

Offline ca0s

  • VIP
  • Sir
  • *
  • Posts: 432
  • Cookies: 53
    • View Profile
    • ka0labs #
Re: pointers and 2d array
« Reply #3 on: February 08, 2016, 12:00:02 pm »
If you cannot know the size at compile time, you will have to pass it to the function and then use pointer arithmetic. Or use an array of pointers to 1-D arrays, which you will have to allocate.

Code: [Select]
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

void func_static(uint64_t size_1, uint64_t size_2, uint64_t *array)
{
uint64_t x, y;
for(x = 0; x < size_1; x++) {
for(y = 0; y < size_2; y++) {
printf("%d\n", array[x * size_2 + y]);
}
}
}

void func_dynamic(uint64_t size_1, uint64_t size_2, uint64_t **array)
{
uint64_t x, y;
for(x = 0; x < size_1; x++) {
for(y = 0; y < size_2; y++) {
printf("%d\n", array[x][y]);
}
}
}

int main()
{
uint64_t array_static[2][4] = { { 1, 2, 3, 4 }, {5, 6, 7, 8} };

uint64_t **array_dynamic;
uint64_t size_1 = 2;
uint64_t size_2 = 4;

uint64_t x, y;

func_static(sizeof(array_static)/sizeof(array_static[0]), sizeof(array_static[0])/sizeof(array_static[0][0]), (uint64_t *)&array_static);

array_dynamic = malloc(size_1 * sizeof(uint64_t));
for(x = 0; x < size_1; x++) {
array_dynamic[x] = malloc(size_2 * sizeof(uint64_t));
}
for(x = 0; x < size_1; x++) {
for(y = 0; y < size_2; y++) {
array_dynamic[x][y] = array_static[x][y];
}
}
func_dynamic(size_1, size_2, array_dynamic);

return 0;
}

Offline vegiraghav

  • /dev/null
  • *
  • Posts: 5
  • Cookies: 0
    • View Profile
Re: pointers and 2d array
« Reply #4 on: February 08, 2016, 05:39:41 pm »
When you assign a pointer to an array it Only points to the address of the first element.
Since the first element is an int variable the pointer Will be an integer whose size is 4 bytes
And simply assign the pointer variable to the array as I said it will point only to the first element of the array.
And if passing pointers to the function is ur sole moto then u don't even need to use a pointer variable. C++ does that for u.
For example
If the prototype of the function is
Void fun(int a[ 100][ 100])
Then simply call the function using the array ar  by
Fun(ar);
The when u pass the array ar only its pointer is passed ar can also be a dynamic array .
Hope this helps

« Last Edit: February 09, 2016, 02:04:58 pm by vegiraghav »