![]() |
| ||
| 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> |
| ||
| 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. |
| ||
| 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 7outside 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. |
| ||
| Re: Need help changing the address of an array Not to be picky, but #define NUMBERSis a pre-processor directive and doesn't get scope. (But it should have been outside the function anyway.) |
| ||
| Re: Need help changing the address of an array Quote:
|
| ||
| 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 |
| ||
| Re: Need help changing the address of an array Quote:
|
| ||
| 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; } |
| ||
| 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. |
| ||
| 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