Questions tagged [pointer-to-array]
56 questions
24
votes
2 answers
Allocating less memory than the specified size of a pointer-to-array
In C, is it "legal" to under-allocate memory to a pointer-to-array if we then only access elements that fall within the allocated memory? Or does this invoke undefined behavior?
int (*foo)[ 10 ]; //Pointer to array of 10 ints
foo =…

Jackson Allan
- 727
- 3
- 11
8
votes
5 answers
What is a pointer to array, int (*ptr)[10], and how does it work?
int (*ptr)[10];
I was expecting ptr to be an array of pointers of 10 integers.
I'm not understanding how it is a pointer to an array of 10 integers.

rahulvuppala v
- 91
- 1
- 4
8
votes
3 answers
Using and dereferencing (void**)
I would like to pass a "polymorphic" array of pointers to a function.
I can do the following without warnings:
foo (void* ptr);
bar()
{
int* x;
...
foo(x);
}
gcc apparently automatically casts x to a (void*), which is just dandy.
However, I…

crockeea
- 21,651
- 10
- 48
- 101
8
votes
2 answers
How to get size of 2D array pointed by a double pointer?
I am trying to get the number of rows and columns of a 2D Array from a double pointer pointed to the array.
#include
#include
void get_details(int **a)
{
int row = ??? // how get no. of rows
int column = ??? // how…

Yash
- 93
- 1
- 2
- 4
4
votes
3 answers
Why is the result of using `int (*p)[5]` so confusing?
I know that int (*p)[5] means a pointer which points to an array of 5 ints.
So I code this program below:
#include
using namespace std;
int main()
{
int a[5]={0,1,2,3,4};
int (*q)[5]=&a;
cout<

Archeosudoerus
- 1,101
- 9
- 24
4
votes
3 answers
If int (*p_arr)[10] is defined as a pointer to an array of size 10 then why compiler shows warning in this case?
I have this code:
#include
int main()
{
int arr[10] = {0};
int *p1_arr = arr;
int (*p2_arr)[10] = arr; // Line 7, Shows Warning here
...
return 0;
}
On compiling on gcc using gcc -g -Wall LengthofArray.c, it…

0xF1
- 6,046
- 2
- 27
- 50
3
votes
0 answers
GDB misses to display const qualifier?
Give the following source (main.c):
void foo(const char (*pa)[4])
{
}
int main(void)
{
const char a[4] = "bar";
foo(&a);
}
... compiled with GCC (gcc (Debian 4.9.2-10) 4.9.2) and run under GDB (GNU gdb (Debian 7.7.1+dfsg-5) 7.7.1) ...
(gdb) b…

alk
- 69,737
- 10
- 105
- 255
3
votes
1 answer
Array pointer in C program
Here is a C program in textbook, it asks a 3*5 2D array from users and prints the third line.
I am confused with int* p[5]. Why here needs to have [5], I think just int* p is OK. It can repeatedly add and point to the next memory space in the int…

Jennifer Q
- 257
- 3
- 12
3
votes
1 answer
Pointer to array and errors C2057, C2540
I want to do something like:
const int N = 10;
void foo (const int count)
{
int (* pA) [N][count] = reinterpret_cast(new int[N * count]);
...
}
But my compiler (VS2010) doesn't want to do it:
error C2057: expected constant…

user1234567
- 3,991
- 3
- 19
- 25
2
votes
1 answer
Initializing pointer to array without & operator?
In the following pointer to array,
int x[5] = {1};
int (*a)[5] = &x;
what implications and consequences can i have if i don't use & operator and do like this..
int x[5] = {1};
int (*a)[5] = x; //No & operator
Can anyone explain with some good…

Armaan Saxena
- 31
- 4
2
votes
2 answers
How to define and use a pointer to an "array" member?
I have a bunch of structs, each of which has an 'array' member and a size indicator:
struct S {
size_t num;
int arr[100];
};
struct V {
float array[10];
int size;
};
I want to create manipulator objects for each struct:
template…

Benji Mizrahi
- 2,154
- 2
- 23
- 38
2
votes
2 answers
How to use pointer to multi dimentional array in C language?
I have 2-D array
char arr[2][3]={"sam","ali"}
and pointer to this array
char(*ptr)[3]=arr;
How can I use this pointer to print arr[2][2] which in this case is i.
I've tried * (*(ptr+1)+2) the same way I am dealing with the array but didn't work so…

Mohamed Moustafa
- 95
- 2
- 8
2
votes
2 answers
While dereferencing pointer to an array I am getting same address as that of pointer to an array
#include
int main(void)
{
int arr[5]={1,2,3,4,5};
int (*ptr)[5]=&arr;
printf("ptr=%p\n",ptr); i am not getting the diff btw both statements
printf("*ptr=%p\n",*ptr);
return 0;
}
output:-…

sam1006
- 103
- 1
- 8
2
votes
0 answers
Array of Pointers to 2d Arrays
I have 5 different 2d Arrays in contrast to the normal usage of 2d Arrays.
int A[1][2] = {2, 5};
int B[1][2] = {6, 1};
int C[1][2] = {4, 8};
int D[1][2] = {3, 6};
int E[1][2] = {9, 7};
Then I have declared an array of Pointers to these 5 arrays by:…

Mohammad Sohaib
- 577
- 3
- 11
- 28
2
votes
6 answers
How is pointer to array different from array names?
I was reading more about arrays vs pointers in C and wrote the following program.
#include
int arr[10] = { } ;
typedef int (*type)[10] ;
int main()
{
type val = &arr ;
printf("Size is %lu\n", sizeof(val)) ;
printf("Size of…

Pratik Singhal
- 6,283
- 10
- 55
- 97