Skip to contentSkip to conversation

pointers

DISCUSSION ework0 2 participants 2 messages Oct 30, 2005 – Oct 31, 2005

lore
Share & tools
lore

ework0

author
#0 lore raw

Hello,

I would like to know what are the advantages of use pointers to insert/modify values in an array of characters for example.

char myword[2];
*myword = 'h';
*(myword + 1)='i';

why would i use this ( the intruction: *(myword + offset))?
what is the advantages of use this manipulation of array with these pointers ? In the last line, do I increment the address that contains what s points to right ?? Why I increment it in '1' instead of a memory word concept?

Thanks

#1 lore raw
Quoted 2 lines · On Sunday 30 October 2005 09:11, ework0 wrote:

I would like to know what are the advantages of use pointers to insert/modify values in an array of characters for example.

With a pointer you can have access to a single element of an array in a very easy and fast way. Probably your example was not the best for this purpose; considere this one:

char str[16] = "0123456789ABCDEF";
char* p = str;

Now p points to the first element of the array str. If you want to obtain or to modify the element pointed by p, you can do this by simply operating on p. Remember that the operations like p++, will not modify the element pointed by p, but only p will point the next element in the array.

Quoted 2 lines

what is the advantages of use this manipulation of array with these pointers ?

When you have to scan big sequences of objects, is a very common way to use pointers in order to have the access to a single element or when you pass an object to a function passing the address and not to copy the entire object.

A wrong use of pointers can cause errors difficult to find.

Signature · 5 lines
With regards,


					HIToC
					[email protected]