My Name is Leonard E. Norwood Jr. I'm Undergraduate student from Norfolk State University in Norfolk Virginia.

Getting down to the point, my program is supposed to print out a list of pre-set numbers in reversal. I used pointers for this, however, something went wrong so I'm trying my best to trace this program and assume that there is something wrong with one of the for loops that is causing something to happen. This program can compile but nothing is happening, I just need some slight assitance.

#include <stdio.h>

{
int i, j;
float ar[5] = {12.75, 18.34, 9.85, 23.78, 16.96}, br[5];
float *ptrAr, *ptrBr;
ptrAr = ar;
ptrBr = &br[4];
for ( i = 0; i < 5; i++) {
*ptrBr = *ptrAr;
ptrAr++;
ptrBr--;
}
for ( i = 0; i < 5; i++) {
printf("%5.2f\n", *ptrBr);
ptrBr++;
}
return 0;
}

There doesn't seem to be a main function in this program. I'll guess that you do actually have int main() before line one, above. When I run it, it does this:

 0.00
16.96
23.78
 9.85
18.34

Do you not get anything out? Your code has one problem; when you start printing out the br array, you're starting at br[-1], so to speak, when you should start at br[0].

this is because your last iteration make ptrBr point to some unknown location(i.e point to 1 location back).
let's see how your iteration works

iteration no      ptrAr         ptrBr
iteration 1       a[1]           a[3]
iteration 2       a[2]           a[2]
iteration 3       a[3]           a[1]
iteration 4       a[4]           a[0]
iteration 5       some unkown address becuase in this case index can't be exceed to 4 and can't be less than 0. therefore i think both point to +1(for ptrAr) and -1 (for ptrBr)

therefore after the loop , add the following statement for desierd output

ptrBr++;
ptrAr--;

At the end of the first loop, ptrBr points to a memory location before br[0]. You need to either increment ptrBr or reset it:

#include <stdio.h>

int main(void)
{
    int i, j;
    float ar[5] = {12.75, 18.34, 9.85, 23.78, 16.96}, br[5];
    float *ptrAr, *ptrBr;
    ptrAr = ar;
    ptrBr = &br[4];
    for ( i = 0; i < 5; i++) {
        *ptrBr = *ptrAr;
        ptrAr++;
        ptrBr--;
    }
    ptrBr = br;
    for ( i = 0; i < 5; i++) {
        printf("%5.2f\n", *ptrBr);
        ptrBr++;
    }
    return 0;
}

And if your goal is simply to print the array in reverse, no copying is necessary:

#include <stdio.h>

int main(void)
{
    float ar[5] = {12.75, 18.34, 9.85, 23.78, 16.96};
    float *ptrAr = &ar[4];
    int i;

    for (i = 0; i < 5; i++)
    {
        printf("%5.2f\n", *ptrAr--);
    }

    return 0;
}

Sorry for the absent, I was busy with the preparation for the cookout. Anyway, I just traced this program, and some of you guys might be correct, it seems the program I made, something out of scope with br[], Learner10, is it? Yeah, I went through the trace and found that you described.I did it over one more time. And and your right. That was the problem I saw. I just need to get used to tracing in case I have any problems

it seems the program I made, something out of scope with br[], Learner10, is it?

yes , it(ptrBr) points to memory location just below br[0].Thats why we need to increment it again and therefore use ++ with ptrBr.

Member Avatar for iamthwee

Why bother running the gaunlet of pointer notation? Just use an array offset, plus it is much easier to read... array[i].

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.