pointer-to-pointer

Pass multiple-dimensional array to a function in C

让人想犯罪 __ 提交于 2019-12-20 03:32:07
问题 I have a function like this: void myfunc(int** arr, int n) { int i, j; for(i=0; i<n; ++i) { for(j=0; j<n; ++j) { printf("%d,", *(arr + i*n + j) ); // Print numbers with commas } printf("\n"); // Just breakline } } in other function I have an two-dimensional array like this: int main() { int seqs[8][8] = { {0, 32, 36, 52, 48, 16, 20, 4}, {0, 16, 20, 52, 48, 32, 36, 4}, {0, 32, 36, 44, 40, 8, 12, 4}, {0, 8, 12, 44, 40, 32, 36, 4}, {0, 32, 36, 38, 34, 2, 6, 4}, {0, 2, 6, 38, 34, 32, 36, 4}, {0,

Dynamic memory allocation for pointer arrays

本秂侑毒 提交于 2019-12-18 12:12:57
问题 I'm am trying to write a program that reads in a series of strings from a text file and stores these in an array of strings, dynamically allocating memory for each element. My plan was to store each string in an array using a pointer and then grow the array size as more were read in. I am having trouble to understand why my test code below is not working. Is this a workable idea? char *aPtr; aPtr =(char*)malloc(sizeof(char)); aPtr[0]="This is a test"; printf("%s",aPtr[0]); 回答1: In C a string

Is this a pointer to a pointer of the start of an array?

六眼飞鱼酱① 提交于 2019-12-17 03:44:49
问题 I've just been helping someone out with some code. He had this: char dataArray[10]; Then wanted to get a pointer to the start of the array. Rather than use: &dataArray[0] or just dataArray He used &dataArray Did he end up with a pointer to a pointer there? I'm not sure what &dataArray would give him. 回答1: &dataArray[0] is of type char * . That is a pointer to char . dataArray is of type char[10] &dataArray will be of type char (*)[10] . That is a pointer-to-array. Apart from that, the value

Difference between pointer to pointer and pointer to 2d array

坚强是说给别人听的谎言 提交于 2019-12-14 03:42:31
问题 If I have a 2d array B defined as : int B[2][3] = {{1,3,5},{2,4,6}}; Is int **p = B same as int (*p)[3] = B ? int **f = B; printf("%d ",*f+1); gives 5 as output while printf("%d ",*f) gives 1 as answer. Why is that happening? printf("%d ",**f); returns a segmentation fault! Why? 回答1: No. int **p = B; is an error. (Both a compilation error, and a logical error). An int ** must point to an int * . However, there are no int * stored in B . B is a group of contiguous int s with no pointers

C program, Why dereferece a char pointer to a pointer doesnt get expected value

柔情痞子 提交于 2019-12-12 04:08:15
问题 In this program I have char variable a , b is a pointer to a and c is a pointer to b . While *a=b , *c not equal to b . I don't understand why ,Can anyone explain? Another thing I don't understand I that if I change variable from char to int , dereference c result b value. *c equal to b .But if variable is char type, it does not. #include<stdio.h> #include<string.h> int main() { char a = "a" ; char *b; b = &a; printf("%d\n", b); printf("%d\n\n", &a); printf("Deference b* hold value: %d\n", *b

Double and triple pointers in C

谁说我不能喝 提交于 2019-12-11 03:46:21
问题 I have a small program as shown below.This program is an attempt to better understand pointers in 'C' how variables are arranged in memory. #include <stdio.h> const char *c = "hello"; const char **cp = &c; const char ***cpp = &cp; const char ****cppp = &cpp; int main() { printf("PTR (c) : %p \n",c); printf("PTR (cp) : %p \n",cp); printf("PTR (cpp) : %p \n",cpp); printf("PTR (cppp) : %p \n",cppp); printf("CONTENT (c) : %c \n",*c); printf("CONTENT (cp) : 0x%x \n",*(unsigned int*)cp); printf(

How can a double pointer be used for a two dimensional matrix?

泪湿孤枕 提交于 2019-12-08 01:32:01
问题 I am trying my hand at C by implementing Conway's game of Life. I am trying to dynamically build two grids ( int matrices), one for the current and one for the next generation, so after I determine what the next generation looks like, I just swap pointers. At first I tried hopelessly to define the pointer to the grid like int * grid , which you cannot subscript with a second set of brackets like [][] because - obviously - the first set of brackets returns an int. I also tried something like

How can a double pointer be used for a two dimensional matrix?

心已入冬 提交于 2019-12-06 11:46:07
I am trying my hand at C by implementing Conway's game of Life. I am trying to dynamically build two grids ( int matrices), one for the current and one for the next generation, so after I determine what the next generation looks like, I just swap pointers. At first I tried hopelessly to define the pointer to the grid like int * grid , which you cannot subscript with a second set of brackets like [][] because - obviously - the first set of brackets returns an int. I also tried something like int * grid[HEIGHT][WIDTH] , but this gives problems assigning one pointer like this to another. (And in

Multi-dimensional array and pointer to pointers

半腔热情 提交于 2019-12-05 08:42:54
When you create the multi-dimensional array char a[10][10] , according to my book it says you must use a parameter similar to char a[][10] to pass the array to a function. Why must you specify the length as such? Aren't you just passing a double pointer to being with, and doesn't that double pointer already point to allocated memory? So why couldn't the parameter be char **a ? Are you reallocating any new memory by supplying the second 10. Pointers are not arrays A dereferenced char ** is an object of type char * . A dereferenced char (*)[10] is an object of type char [10] . Arrays are not

Difference between pointer to pointer and pointer to 2d array

柔情痞子 提交于 2019-12-04 17:48:59
If I have a 2d array B defined as : int B[2][3] = {{1,3,5},{2,4,6}}; Is int **p = B same as int (*p)[3] = B ? int **f = B; printf("%d ",*f+1); gives 5 as output while printf("%d ",*f) gives 1 as answer. Why is that happening? printf("%d ",**f); returns a segmentation fault! Why? No. int **p = B; is an error. (Both a compilation error, and a logical error). An int ** must point to an int * . However, there are no int * stored in B . B is a group of contiguous int s with no pointers involved. int **f = B; must give a compilation error. The behaviour of any executable generated as a result is