-3

I learned pointers and arrays already but I don't know what ** or && is used for and when trying to understand this line im not sure what this is.

int main() {
    srand(time(NULL));
    int n, m, option, row, col, flagCount = 0;
    char **flags, **board;

im not sure what's being declared in the 4th row also those pointers at list I think they are are later in the program being sent to this function .

char **creatArray(int n) 

what is this (**) used for in the function type?

  • 4
    Possible duplicate of [Why use double pointer? or Why use pointers to pointers?](https://stackoverflow.com/questions/5580761/why-use-double-pointer-or-why-use-pointers-to-pointers) – CloudPotato May 12 '19 at 13:24
  • I would edit my post but I can't sorry, its telling me that there's spam from my network something like that. – yadin meretzki May 12 '19 at 13:56

3 Answers3

2

* is a pointer type, ** is a pointer type to a pointer, for example: int* p;, meaning that p is a pointer type that points to int, and if you need a pointer type to this p pointer, you can do the following definition: int** test = &p; At this point test is a pointer to p, its type is int**.

alk
  • 69,737
  • 10
  • 105
  • 255
poppy Luc
  • 33
  • 5
1

char *flag; it means that flag is a pointer which will hold the address of character data type

char **flag; it means that flag is a pointer which will hold the address of a "POINTER" which is pointer to a character data type enter image description here

-1

When you use a pointer '*' you can create for example an array when you allocate memory for multiple chars. Example char *word = malloc(10*sizeof(char)). Your word has now space for 10 chars. When you want to create a sentence you need multiple words so you can create a two dimensional array. You can say char *sentence = malloc(10*sizeof(word)).

In c double pointer are often used to create a two dimensional array.

When your function create a new game board of size n*n then it will return the array. So for example you call char **board = **newArray(n);

In c you can create an array like char array[] or you can create an like char *array. This is the same because an array in C is a pointer to the first element of the array. But the problem is you can't return an array like this from a function you need to return the pointer. So char[] function() is not working you need to say char *function(). And this is the case in your example you create a new two dimensaional array for your board and return the pointer to the first element (pointer) of the array that is why you call your function char** and not char[][].

Moschte
  • 102
  • 1
  • 11
  • I know what its used for (basically a board for a game) but im more confused about why its in the function name – yadin meretzki May 12 '19 at 13:44
  • When you create a game board with this function I think you pass the argument n to create a n * n sized board. So your function create a new Array/gameBoard and return this game board as a two dimensional array. So the code should look something like char **board = createArray(n) – Moschte May 12 '19 at 13:48
  • yes that pretty much what I figured it did except that this is the line board = creatArray(n); so why are there no ** here? – yadin meretzki May 12 '19 at 13:51
  • and also why would I need a pointer to a pointer array to make a board game. this isn't my code I just needed help with understanding it but why now use a normal two dimension array? – yadin meretzki May 12 '19 at 13:53
  • You need a pointer to a pointer array because you need the two dimensions – Moschte May 12 '19 at 14:02
  • can't you make a two dimensional array that has nothing to do with pointers? – yadin meretzki May 12 '19 at 14:07
  • No you can't. When you create an array in c you allocate coherently memory. For example when you create an array for 10 integers you allocate coherently memory with enough space for 10 integers an your array is a pointer to the first address of this array when you call array[1] c calculate the first address plus the size of one integer to get your second value from the array. In short in c your array is a pointer to the first array Element so you can't create an array without a pointer – Moschte May 12 '19 at 14:11
  • You simply simplify to much. An array is not a pointer and a pointer is not an array. It's just that arrays in most cases are decayed to a pointer to their 1st element. – alk May 12 '19 at 14:15
  • @yadinmeretzki: "*can't you make a two dimensional array that has nothing to do with pointers*" sure. For a 2D `char` array with 3x5 elements just define: `char array2D[3][5]`. – alk May 12 '19 at 14:17
  • @alk it's not simplify it's the Definition of c. An array is a pointer to the first address of coherently allocated memory – Moschte May 12 '19 at 14:21
  • @alk you can do that as i mentioned in my answer but the problem is you can't return this from your function you need a pointer you can't return an array. – Moschte May 12 '19 at 14:25
  • "*you can't return this from your function you need a pointer you can't return an array.*" Correct. And this BTW is a prefect prove for that arrays and pointers are different beasts. ;) – alk May 12 '19 at 14:27
  • Another thing with you answer: Assuming `newArray(n)` returned `char**` then this `char **board = **newArray(n);` won't compile nicely. – alk May 12 '19 at 14:30
  • Also following this advice "*In c double pointer are often used to create a two dimensional array.*" you do not get a real array. But a jagged (aka scattered) "array", that is you do not get a continuous region of memory, but several separated regions. – alk May 12 '19 at 14:33
  • @alk they are not exact the same no but I think this is not part of the question – Moschte May 12 '19 at 14:33
  • 1
    @alk i think the correct syntax is char **board = newArray(n). If this is false please correct me – Moschte May 12 '19 at 14:35
  • @alk since I am programming C this is the most useful pro of using pointer to pointer. And as far as I know the only way to create a dynamic array in C – Moschte May 12 '19 at 14:36