943,514 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 698
  • C RSS
Dec 28th, 2008
0

Need help changing the address of an array

Expand Post »
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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
RenFromPenn is offline Offline
40 posts
since Dec 2008
Dec 28th, 2008
0

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Dec 28th, 2008
0

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.
Last edited by Aia; Dec 28th, 2008 at 12:59 pm.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Dec 28th, 2008
1

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.)
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Dec 28th, 2008
0

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?
Last edited by RenFromPenn; Dec 28th, 2008 at 5:48 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
RenFromPenn is offline Offline
40 posts
since Dec 2008
Dec 28th, 2008
0

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
Last edited by Aia; Dec 28th, 2008 at 6:01 pm.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Dec 28th, 2008
0

Re: Need help changing the address of an array

Click to Expand / Collapse  Quote originally posted by Aia ...
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.
Reputation Points: 10
Solved Threads: 0
Light Poster
RenFromPenn is offline Offline
40 posts
since Dec 2008
Dec 29th, 2008
0

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;

}
Reputation Points: 10
Solved Threads: 1
Newbie Poster
somnathsarode is offline Offline
7 posts
since Dec 2008
Dec 29th, 2008
0

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.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Dec 29th, 2008
0

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. :-)
Reputation Points: 10
Solved Threads: 0
Light Poster
RenFromPenn is offline Offline
40 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Challange in C
Next Thread in C Forum Timeline: Algorithm for EBCDIC to ASCII Conversion





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC