Reputation: 661
I think I will understand this concept better if someone can assist me in a current project that I'm working on. I want to use C to edit data at specific memory addresses, using pointers. Specifically, I have two character arrays (strings) which I need to both read data from at specific locations, and also write to at specific locations.
I'm mostly confused about syntax of pointers, such as *
and ->
and &
.
From my understanding, the *
refers to the data kept at the current memory address of the pointer. So, for example, if I wanted to edit the data at the beginning memory address of a char *p
, I would do something like: (*p) = 'c';
Now, what if I wanted do to alter a character at the 2nd memory address from the beginning of p
?
Also, I understand that &
refers to the location of the pointer. But I don't know how to use this syntax.
Here is my example:
int orig_length = strlen(original_string); //-1 for \0?
char *poriginal, *pnew_string;
poriginal = &original_string;
while(orig_length>0) {
k = 0;
j = 0;
while(isalpha(*(poriginal+j))) {
j++;
k++;
}
while(k > 0) {
*(pnew_string+(j-k)) = toupper(*(poriginal+k-1)); //toupper
k--;
}
if(*(poriginal+(j)) == '_') {
*(pnew_string+(j)) = ' ';
}
else {
*(pnew_string+(j)) = *(poriginal+(j));
}
orig_length = orig_length - j;
}
*(pnew_string+strlen(pnew_string)) = '\0'; //Syn? Is this actually necessary?
... //program continues...
By the way, this program is meant to take one string "now_i_understand!" and reverse each word, capitalize each word, switch _ to ' ', and leave other punctuation alone: "WON I DNATSREDNU!"
Upvotes: 0
Views: 975
Reputation: 263257
The *
and &
operators are inverses of each other. A pointer object hold the address of some other object in memory (or it old a null pointer, which doesn't point to any object).
The unary *
operator takes a pointer operand, and gives you the object that it points to; this is called dereferencing.
The unary &
operator takes an operand that refers to an object of any type, and gives you a pointer to that object. &
is the address-of operator.
For example:
int obj = 42; /* obj is an object of type int; it currently holds the value 42 */
int *ptr; /* ptr is a pointer to an int */
ptr = &obj; /* ptr now holds the address of obj */
printf("obj = %d\n", obj); /* prints 42 */
printf("*ptr = %d\n", *ptr); /* also prints 42; *ptr is another name for obj */
The ->
operator is shorthand for dereferencing a pointer and accessing a member of what it points to. The prefix must be a pointer to a struct or union. foo->bar
means the same thing as (*foo).bar
, where foo
is a pointer and bar
is the name of a member of what foo
points to.
You can also perform arithmetic on pointers. If ptr
is a pointer pointing to an element of an array, then ptr + 1
points to the next element of the array, ptr + 2
points to the element after that, and so forth.
The []
array indexing operator is actually defined in terms of pointer arithmetic. ptr[2]
means exactly the same thing as *(ptr+2)
. Combine that with the fact that an array name, in most contexts, decays to a pointer to the array's first element, and with a little thought you'll see how arr[2]
refers to the third element of the array arr
(third because indexing starts at 0
).
I strongly recommend sections 4 (Pointers) and 6 (Arrays and Pointers) of the comp.lang.c FAQ; it will likely explain this stuff better than I have.
Upvotes: 1
Reputation:
If what you are dealing with is an array of characters (and it is), use array syntax:
pnew_string[j+1] = poriginal[j+1];
Note that this syntax is equivalent to:
*(pnew_string + j + 1) = *(poriginal + j + 1);
but is more readable.
Dealing with most of the other cases you've got should be obvious given this example.
Upvotes: 3