Search results
Results from the WOW.Com Content Network
If you think of them as pointers, you'll be using * to get at the values inside of them as explained above, but there is also another, more common way using the [] operator: int a[2]; // array of integers. int i = *a; // the value of the first element of a. int i2 = a[0]; // another way to get the first element.
Function pointers in C can be used to perform object-oriented programming in C. For example, the following lines is written in C: String s1 = newString(); s1->set(s1, "hello"); Yes, the -> and the lack of a new operator is a dead give away, but it sure seems to imply that we're setting the text of some String class to be "hello".
Instead you use this (*ptr).kg and you force compiler to 1st dereference the pointer and enable acess to the chunk of data and 2nd you add an offset (designator) to choose the member. Check this image I made: But if you would have nested members this syntax would become unreadable and therefore -> was introduced.
Since Strings in C are char pointers, and you want to swap Strings, you are really swapping a char pointer. As in the examples with an int, you need a double pointer to swap addresses. The values of integers can be swapped even if the address isn't, but Strings are by definition a character pointer.
C distinguishes between object pointers and function pointers (void * is an object pointer), and C does not allow conversion between them. If you turn compiler diagnostics up to -pedantic level you should see warnings.
Third, by increasing the value of a pointer, you're incrementing it by the sizeof its contents, that is you're incrementing it as if you were iterating in an array. So, to sum it all up: *ptr++; // Pointer moves to the next int position (as if it was an array). But returns the old content.
Now cp points to c, that is, it contains the address of c (which is 58). We can go even further. Consider: const char ***cpp = &cp; Now cpp stores the address of cp. So it has value 55 (based on the example above), and you guessed it: it is itself stored at address 60. As to why one uses pointers to pointers:
Casting pointers is usually invalid in C. There are several reasons: Alignment. It's possible that, due to alignment considerations, the destination pointer type is not able to represent the value of the source pointer type. For example, if int * were inherently 4-byte aligned, casting char * to int * would lose the lower bits. Aliasing.
C and C++ allows the use of pointers that point to pointers (say that five times fast). Take a look at the following code: char a; char *b; char **c; a = 'Z'; b = &a; // read as "address of a" c = &b; // read as "address of b" The variable a holds a character. The variable b points to a location in memory that contains a character.
Passing pointers is the workaround. Pass By Value: void fcn(int foo) When passing by value, you get a copy of the value. If you change the value in your function, the caller still sees the original value regardless of your changes. Pass By Pointer to Value: void fcn(int* foo)