here are my questions 1)what is the difference *p and *[p] which one passes the
ID: 3622544 • Letter: H
Question
here are my questions1)what is the difference *p and *[p] which one passes the character to the next function as shown below
2) why do you increment p by doing p++ instead of *p++.
Say the pointer points to a string ended by an null. Im in a function with the string and i am outputting the each character by passing the char to another function.
so
void char_out(char){//output}
void String_Out(*p){//pointer to string im in this one.
while(*p); // why dont we do *[p] != null
char_out(*p);// why do we do this instead of *[p]
p++;// we do we increment this way?
}
Explanation / Answer
specifically, *p++; incremented the address instead of the value the pointer was pointing too. this makes *p++; not equal to *p = *p +1; The post increment operator has higher precedence than the dereference operator, so the pointer will be incremented, not the value. However, since it's a post increment, the value will be dereferenced prior to the pointer increment. In other words: someInt = *p++ would place p's appointed value in someInt, then increment p. I wrote a little routine that does some pointer math as a demo for someone, it contains the for loop shown below: Code Snippet for(i = 0; i < 10; i++) { j++; //Value Type Int *p = *p + 1; //Increment referenced Value!! p++; //Native pointer we know and love - We can do pointer math *p++; //Increment Pointer NOT VALUE!!!!! m++; //Managed type - Incement Value managed type ok *n = *n +10; //Increment Value Handle is pointing to - // *n++; //Cant do this. //n++; // Cant do this to a managed Handle //n = n + 1; // Cant do this to a managed Handle }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.