Need help changing the address of an array

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2008
Posts: 40
Reputation: RenFromPenn is an unknown quantity at this point 
Solved Threads: 0
RenFromPenn RenFromPenn is offline Offline
Light Poster

Need help changing the address of an array

 
0
  #1
Dec 28th, 2008
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?

  1. #include <stdio.h>
  2. int print(int[]);
  3.  
  4.  
  5. int main()
  6. {
  7. #define NUMBERS 7
  8. int nums[] = {2, 4, 5, 7, 9, 11, 13};
  9.  
  10. print(nums);
  11.  
  12. return 0;
  13. }
  14.  
  15. int print (int *nums)
  16. {
  17. int i;
  18. i = *nums++;
  19.  
  20. for (i = 1; i < NUMBERS; i++, nums++)
  21. printf("Address %d contains number %d\n", i, *nums );
  22.  
  23. return 0;
  24.  
  25. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Need help changing the address of an array

 
0
  #2
Dec 28th, 2008
> 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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Need help changing the address of an array

 
0
  #3
Dec 28th, 2008
>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.
Last edited by Aia; Dec 28th, 2008 at 12:59 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: Need help changing the address of an array

 
1
  #4
Dec 28th, 2008
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.)
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 40
Reputation: RenFromPenn is an unknown quantity at this point 
Solved Threads: 0
RenFromPenn RenFromPenn is offline Offline
Light Poster

Re: Need help changing the address of an array

 
0
  #5
Dec 28th, 2008
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?
Last edited by RenFromPenn; Dec 28th, 2008 at 5:48 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Need help changing the address of an array

 
0
  #6
Dec 28th, 2008
>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
Last edited by Aia; Dec 28th, 2008 at 6:01 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 40
Reputation: RenFromPenn is an unknown quantity at this point 
Solved Threads: 0
RenFromPenn RenFromPenn is offline Offline
Light Poster

Re: Need help changing the address of an array

 
0
  #7
Dec 28th, 2008
Originally Posted by Aia View Post
start i in the loop at 0 or stop it when is < or equal to NUMBERS
Thanks! < or equal did just what I wanted.
Last edited by RenFromPenn; Dec 28th, 2008 at 6:05 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 7
Reputation: somnathsarode is an unknown quantity at this point 
Solved Threads: 1
somnathsarode's Avatar
somnathsarode somnathsarode is offline Offline
Newbie Poster

Re: Need help changing the address of an array

 
0
  #8
Dec 29th, 2008
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;

}
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: Need help changing the address of an array

 
0
  #9
Dec 29th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 40
Reputation: RenFromPenn is an unknown quantity at this point 
Solved Threads: 0
RenFromPenn RenFromPenn is offline Offline
Light Poster

Re: Need help changing the address of an array

 
0
  #10
Dec 29th, 2008
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. :-)
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC