-1

This code only looks like this as the use of structs and global variables is prohibited in the assignment.

It works either way, but I was wondering whether only using a double pointer would reallocate a temporary copy of the pointer.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void allocateData(char ***dptrArrayData, int *intArraySize)
{
    *dptrArrayData = (char**)malloc(*intArraySize*sizeof(char*));
    for (int i = 0; i < *intArraySize; i++)
    {
        (*dptrArrayData)[i] = (char*)malloc(12*sizeof(char));
        (*dptrArrayData)[i] = "Lorem Ipsum";
    }
}

void printDataDoublePointer(char **dptrArrayData, int intArraySize)
{
    for (int i = 0; i < intArraySize; i++)
    {
        printf("Text:%s at index:%d\n",dptrArrayData[i],i);
    }
}

void printDataTriplePointer(char ***dptrArrayData, int *intArraySize)
{
    for (int i = 0; i < *intArraySize; i++)
    {
        printf("Text:%s at index:%d\n",(*dptrArrayData)[i],i);
    }
}
int main()
{
    char **dptrArrayData;
    int intArraySize = 5;
    allocateData(&dptrArrayData,&intArraySize);
    printDataDoublePointer(dptrArrayData,intArraySize);
    printDataTriplePointer(&dptrArrayData,&intArraySize);
    return 0;
}
  • The function parameter needs to have one more `*` than the variable that it's updating. – Barmar Nov 02 '21 at 00:12
  • You could use a `char **` return value instead of `char ***` parameter. – Barmar Nov 02 '21 at 00:13
  • The question isnt about updating, and sadly I cannot use a return value as I will return many arrays in one function, the question is whether using the double pointer to only read is the same as using a triple pointer. –  Nov 02 '21 at 00:17
  • 1
    What is it about? `allocateData()` needs to update the variable `dptrArrayData`, so it needs `char ***`. The other functions don't update anything, so they can use `char **`. – Barmar Nov 02 '21 at 00:20
  • So when I pass in the double pointer only it doesnt create a temporary copy of the array to read from? Because if I update something in the double pointer one the value stays in scope. Edit: I am a noob at c, oop is all I know... –  Nov 02 '21 at 00:22
  • (*dptrArrayData)[i] = (char*)malloc(12*sizeof(char)); (*dptrArrayData)[i] = "Lorem Ipsum"; This is nonsense. – gnasher729 Nov 02 '21 at 00:29
  • I put it as an example, but this is how you allocate a dynamic 2D char array if you have a triple pointer passed, am I wrong? The assignment of the value is only as a test. –  Nov 02 '21 at 00:31

1 Answers1

0

It works either way, but I was wondering whether only using a double pointer would reallocate a temporary copy of the pointer.

I take you to be asking about the difference between

void printDataDoublePointer(char **dptrArrayData, int intArraySize)

and

void printDataTriplePointer(char ***dptrArrayData, int *intArraySize)

. In each case the function receives copies of all argument values presented by the caller. This is the essence of pass-by-value semantics, which are the only function-call semantics C provides.

But there is no reallocation involved. When a pointer is passed as a function argument, only the pointer is copied, not the object to which it points. The function receives an independent pointer to the same object the original points to.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • Thank you, I didnt know that, maybe they should tell us this at school lol –  Nov 02 '21 at 00:27
  • 2
    It's right there in the name: "pointer". It's just something that points to something else, not the thing itself. – Barmar Nov 02 '21 at 00:29