Curnelious Curnelious - 5 months ago 19
C++ Question

Why array passed to function is null?

I know this is basic, but I think of this logically, and don't get this.

char *me[] = {0};
printf("**%s\n", me[0]); // null

void EEprom::getHardwareEEprom(char *eeprom[])
char *EEPROM[]={"A4", "B3", "C=AB", "if(C7)", "(C)", NULL};
eeprom = EEPROM; // set the address for the data array

  1. me
    is a pointer that points to some place

  2. its passed to this function, where it gets an address of some array in memory

  3. me
    holds now the address for the array

  4. printing the first word of it show null

No matter how much I read, I just do not seems to get this thing.


There's nothing particularly special going on here.

You are changing what the function parameter eeprom is assigned to, that's all. And that assignment is valid for the lifetime of EEPROM.

But that change is not reflected in the caller (C++ is a pass by value language). So me in the caller stays as it was.

It would have been a different matter entirely had the function parameter eeprom had been passed by reference.