-3

Suppose I have this example function

int main(int argc, char** argv) {

        char* pChar = argv[2]; // Get the second argument "Word"
        char * pAdd = pChar + strlen(pChar); // 0 + 5
}

and run by inputting ./fileName Hello World

Since argv[2] = World, pChar should point to the memory address of the Char "World"

However, what I don't get is why pChar = 0 (that is what the book says) when executing pChar + strlen(pChar) line of code.

2 Answers2

2

pChar is not 0.

POSSIBLE ERROR: *pAdd is 0 instead of pChar since pAdd is pointing to the last character of string "World" i.e. '\0' and integral value of null termination ('\0') is 0.

You want to say that address is 0 - you might need to read about that:

in c can an address of a pointer be 0

Gaurav
  • 1,570
  • 5
  • 17
1

You need to explain or demonstrate how you made the observation. pChar will not be zero, however it will point to the NUL character at the end of the second argument. i.e *pChar will be NUL - demonstrated by the empty string pointed to by pAdd here:

enter image description here

The upshot is, make your observations using a debugger - it is the least intrusive method and does not rely on you writing debug code correctly.

Clifford
  • 88,407
  • 13
  • 85
  • 165