DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   Need help changing the address of an array (http://www.daniweb.com/forums/thread164589.html)

RenFromPenn Dec 28th, 2008 12:41 pm
Need help changing the address of an array
 
Hi,

I am trying to change the address in an array using *nums++. When I run the program, however, the first number in the array is skipped. What am I missing? How do I get the first number to display?

#include <stdio.h>
int print(int[]);


int main()
{
        #define NUMBERS 7
        int nums[] = {2, 4, 5, 7, 9, 11, 13};

        print(nums);

        return 0;
}

int print (int *nums)
{
        int i;
        i = *nums++;

        for (i = 1; i < NUMBERS; i++, nums++)
        printf("Address %d contains number %d\n", i, *nums );

return 0;

}

Salem Dec 28th, 2008 12:45 pm
Re: Need help changing the address of an array
 
> i = *nums++;
Well this does nothing, except to skip the first entry of the array.
You lose i in the following assignment in the for loop.

Aia Dec 28th, 2008 12:58 pm
Re: Need help changing the address of an array
 
>What am I missing? How do I get the first number to display?

Move
#define NUMBERS 7
outside of main so the function print can see it.
In print() remove
i= *nums++
which is doing harm and producing nothing since you change the value of i inside the loop to start at 1 anyway.

Murtan Dec 28th, 2008 3:28 pm
Re: Need help changing the address of an array
 
Not to be picky, but
#define NUMBERS
is a pre-processor directive and doesn't get scope.

(But it should have been outside the function anyway.)

RenFromPenn Dec 28th, 2008 5:47 pm
Re: Need help changing the address of an array
 
Quote:

Move
#define NUMBERS 7
outside of main so the function print can see it.
In print() remove
i= *nums++
which is doing harm and producing nothing since you change the value of i inside the loop to start at 1 anyway.
I tried this and now I have the opposite problem. The last number doesn't print. Suggestions?

Aia Dec 28th, 2008 5:56 pm
Re: Need help changing the address of an array
 
>I tried this and now I have the opposite problem. The last number doesn't print. Suggestions?

start i in the loop at 0 or stop it when is < or equal to NUMBERS

RenFromPenn Dec 28th, 2008 6:04 pm
Re: Need help changing the address of an array
 
Quote:

Originally Posted by Aia (Post 766087)
start i in the loop at 0 or stop it when is < or equal to NUMBERS

Thanks! < or equal did just what I wanted.

somnathsarode Dec 29th, 2008 1:30 am
Re: Need help changing the address of an array
 
see what i have made changes in u r program




#include <stdio.h>
int print(int[]);


int main()
{
#define NUMBERS 7
int nums[] = {2, 4, 5, 7, 9, 11, 13};

print(nums);

return 0;
}

int print (int *nums)
{
int i;
i = (*nums)++;

for (i = 1; i <= NUMBERS; i++, nums++)
printf("Address %d contains number %d\n", i, *nums );

return 0;

}

Murtan Dec 29th, 2008 3:52 am
Re: Need help changing the address of an array
 
somnathsarode if you are going to post code, especially in response to a thread, please use the appropriate code tags.

For this forum:
[code=c]
/* your code goes here */
[/code]

Your response followed the original poster saying "Thanks ... did just what I wanted." Almost seems like the thread was answered doesn't it.

RenFromPenn Dec 29th, 2008 10:44 am
Re: Need help changing the address of an array
 
Well, since I am the original poster, I can say that my question was answered. I was just about to ask if there was a way to mark this as solved when I looked up and saw it right above this box. Sorry I didn't mark it previously. I'm new hear and still learning how the forum works. :-)


All times are GMT -4. The time now is 3:30 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC