Mombu the Programming Forum

Go Back   Mombu the Programming Forum > Programming > pointers and arrays help
User Name
Password
REGISTER NOW! Mark Forums Read




Reply
1 26th October 15:37
cathzee
External User
 
Posts: 1
Default pointers and arrays help



i have this program that prints out the reversal of the message. i
dontt know if i did it right but my problemis how to revise it by
using a pointer instead of an integer to keep track of the current
position in the array. im totally lost on this part. i would
appreciate the help.
#include <stdio.h>
#define N 100
main()
{
char a[N];
int len=0, i=0;
char ch;
printf("Enter a message: ");
ch=getchar();
while(ch!='\n')
{
a[i]=ch;
i++;

ch=getchar();
}
printf("Reversal is:");
for(a[i]=len; i>=len;--i){
printf("%c", a[i]);
}
printf("\n");
return 0;
}
  Reply With Quote


 


2 26th October 15:38
gianni mariani
External User
 
Posts: 1
Default pointers and arrays help



char * ptr;

Defines a "pointer to char" - it's an address of a char type, a 32 bit
number on most machines.

Type * ptr; is a more general ptr.

There is such a thing as pointer arithmetic.

ptr + intval

and

ptr1 - ptr2

do interesting things - look it up.

in this example (below)

char xxx[N];

char * ptr = xxx;

ptr points to the first element of the array xxx.

ptr = &(xxx[1])

The above shows the "address of operator". In this example ptr is now
pointing to the second element.

Pointers also have the [] operator.

char * ptr = xxx;

&(ptr[3]) == &(xxx[3])

A common thing to do to pointers is increment and decrement them (just
like you would an index of an array).

++ ptr and -- ptr will point to the next and prevous elements in an array.

You can dereference a pointer using
*ptr, ptr[0] and the -> operator if the object pointed to is a struct/class.

char xxx[] = "This is a string";

for( char * ptr = xxx; * ptr; ptr ++ );

will traverse to the nul element in the array (usually the last element
in a nul terminated string (much better to use string lengths if you
have it - like (in this case).

Example of using size.

int size = sizeof( xxx )/sizeof( * xxx ); // for a static array
-or-
int size = std_string.size(); // for a std::string
-or-
int size = strlen( xxx ); // which is depends on the null to terminate

for( char * ptr = xxx; ptr < ( xxx + size -1 ); ptr ++ );

Example of using decrement (reverse traversal).
for( char * ptr = ( xxx + size -1 ) ; ptr >= xxx; ptr -- );

If you digest the information above, you can figure it out.
  Reply With Quote
3 26th October 15:38
karl heinz buchegger
External User
 
Posts: 1
Default pointers and arrays help


thats enourmous complicated. fgets() nearly
does the same thing.

That piece of code is, ugly and misleading. It does
what it should do, but every programmer has to look
very carefully to convice itself that it is indeed correct.


while( ch ...
{
....
}

a[i] = '\0'; /* terminate the character string */ i--;
for( ; i >= 0; --i )
printf( "%c", a[i] );

Now it's clearer, what's going on. Note that the variable 'len'
isn't used anymore. This is especially good, because len doesn't
hold what it's name implies: the length of the string. In what
you have written, len is simply a (misleading) way to write 0 or '\0';


If you have to do the same with pointers, then you first need
a pointer to the last character in the string. Since you know
that there are i charcaters in the string, you get that pointer
with:
char* pTmp = &a[i];

Now that you have that pointer, dereferencing it will yield
the character:

printf( "%c", *pTmp );

All that is left, is to envelope the pirintf in a loop
and decrement the pointer until, you are at the very beginning
of the string. How do you know that you are at the beginning?
Simple: When the pointer equals &a[0], or simpler written a.

char* pTmp = &a[i+1];

do {
--pTmp;
printf( "%c", *pTmp );
} while( pTmp != a );

I changed the start condition to start one behind the last
character (where the '\0' is located), in order to decrement
the pointer as the first action in the loop. This is important
because for the first character in the array, we first have to print
the character, before the test in the while condition terminates
the loop.

--
Karl Heinz Buchegger
kbuchegg@gascad.at
  Reply With Quote
Reply


Thread Tools
Display Modes




666