pointer-to-array

What is a pointer to array, int (*ptr)[10], and how does it work?

霸气de小男生 提交于 2021-02-08 13:43:33
问题 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. 回答1: ptr is of type "pointer to array of 10 int". In your declaration it is uninitialized so it doesn't point to any object. To make it point to an array: int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; // initialize in the declaration: int (*ptr) [10] = &arr; // or after: int (*ptr) [10]; ptr = &arr; 回答2: I like to read it like this: (great

What is a pointer to array, int (*ptr)[10], and how does it work?

╄→гoц情女王★ 提交于 2021-02-08 13:43:11
问题 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. 回答1: ptr is of type "pointer to array of 10 int". In your declaration it is uninitialized so it doesn't point to any object. To make it point to an array: int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; // initialize in the declaration: int (*ptr) [10] = &arr; // or after: int (*ptr) [10]; ptr = &arr; 回答2: I like to read it like this: (great

Using and dereferencing (void**)

非 Y 不嫁゛ 提交于 2019-12-10 03:26:56
问题 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 get a warning when I do the following: foo (void** ptr); bar() { int** x; // an array of pointers to int arrays ... foo(x); } note: expected ‘void **’ but argument is of type ‘int **’ warning: passing argument 1 of ‘foo’ from incompatible pointer type

Using and dereferencing (void**)

做~自己de王妃 提交于 2019-12-05 05:15:01
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 get a warning when I do the following: foo (void** ptr); bar() { int** x; // an array of pointers to int arrays ... foo(x); } note: expected ‘void **’ but argument is of type ‘int **’ warning: passing argument 1 of ‘foo’ from incompatible pointer type [enabled by default] My question is: why is passing an (int*) as a (void*) argument not 'incompatible',

How to get size of 2D array pointed by a double pointer?

可紊 提交于 2019-11-30 15:39:12
问题 I am trying to get the number of rows and columns of a 2D Array from a double pointer pointed to the array. #include <stdio.h> #include <stdlib.h> void get_details(int **a) { int row = ??? // how get no. of rows int column = ??? // how get no. of columns printf("\n\n%d - %d", row,column); } Above function needs to print the details of the size, where am going wrong. int main(int argc, char *argv[]) { int n = atoi(argv[1]),i,j; int **a =(int **)malloc(n*sizeof(int *)); // using a double

How to get size of 2D array pointed by a double pointer?

≡放荡痞女 提交于 2019-11-30 15:16:55
I am trying to get the number of rows and columns of a 2D Array from a double pointer pointed to the array. #include <stdio.h> #include <stdlib.h> void get_details(int **a) { int row = ??? // how get no. of rows int column = ??? // how get no. of columns printf("\n\n%d - %d", row,column); } Above function needs to print the details of the size, where am going wrong. int main(int argc, char *argv[]) { int n = atoi(argv[1]),i,j; int **a =(int **)malloc(n*sizeof(int *)); // using a double pointer for(i=0;i<n;i++) a[i] = (int *)malloc(n*sizeof(int)); printf("\nEnter %d Elements",n*n); for(i=0;i<n