I'm new to pointers in C and I'm having a lot of trouble figuring out how to work with them.
I have this piece of code with some pointer arithmetic inside a void function:
void function(int n, int *a, int *b){
int *p,*q;
q = b;
int i;
*b = 0;
for (i = 1; i<n; i++)
if(*(a+i) == *(a+i-1))
*(b+i)=0;
else
*(b+i)=1;
}
I'm trying to change it completely to use only pointer arithmetic with no loop index variables. So far I've gotten this:
void function(int n, int *a, int *b){
int *p,*q;
q = b;
*b = 0;
for (p = a+1; p<a+n; p++)
if(*a == *a-1)
*b=0;
else
*b=1;
}
But to my understanding isn't it exactly the same as the first piece of code above? Am I missing something? Thanks for your help
Your program have some unused variables. Don't ignore the compiler's warning.
Here is what I guess you mean:
function0()
is syntactically identical towhich is functionally identical to:
If you are struggling with pointers a bit, the short version of how to understand a pointer is to understand it is simply a variable that holds an address to something else as its value. Take a normal variable
int x = 5;
wherex
contains the immediate-value5
.A pointer on the other-hand, does not contain an immediate-value, but contains the address where an immediate-value (or another pointer) can be found in memory. This provides quite a bit of increased flexibility in how you use pointers. Just as you can use arithmetic on
x
(e.g.x++;
) to increment5
->6
, when you increment a pointer to an array, the pointer now points to the next address in the array.So if you have declared
int a[] = {3, 5, 7, 9};
, you would have the following relationships regardinga
among the sequential block of memory allocated as storage fora
:You desire to eliminate working with the array indexes
0-4
and perform the same manipulations infunction
using only the memory address location information and their values. This is where pointer-arithmetic can be used just like array indexing to set, compare, remove values from your array. When iterating an array using a pointer, you generally will declare a pointer to use for iteration purposes so you do not lose the start address for your block of memory.Thankfully when pointers (or arrays due to conversion) are passed as arguments to a function, the function receives a copy of the pointer and not the original address for the pointer in the calling function. You still may need to save a pointer holding the original address for the copy, but that just depends on what you need to do in the function.
If you want to re-implement the same logic in
function
incrementing pointers instead of incrementing an index'i'
, then you will need to handle the increment of the first pointer position in botha
andb
when you set your default*b=0;
. While you can simply skip over the first character in each using indexes by iteratingfor (i = 1;...
, you must increment the pointers themselves if you take that approach. You can handle that within the loop by simply skipping the iteration wherea
holds its original address:Or, you can preincrement both
a
andb
before starting the loop (e.g.a++, b++;
) to insure your are addressing the second and first characters ina
in yourif
statement. An example there would be:Since you seem to have a fair handle on the dereferencing aspect of pointers to operate on the value pointed to in memory, I'll leave that to you. A short comparison example allows you to run the code without argument (or with argument not beginning with
'f'
) to see the results of your original function on the array, or providing an arguments that begins with'f'
will call the pointer use function:Example Use/Output
Look over the function and the example and let me know if you have any questions. I hope this helps.
They are not the same, you have to substitute
a
withp
in thefor
body and use theq
pointer to modifyb
.