| | |
Need help changing the address of an array
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 40
Reputation:
Solved Threads: 0
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?
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?
C Syntax (Toggle Plain Text)
#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; }
>What am I missing? How do I get the first number to display?
Move
In print() remove
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.
•
•
Join Date: Dec 2008
Posts: 40
Reputation:
Solved Threads: 0
•
•
•
•
Move#define NUMBERS 7outside of main so the function print can see it.
In print() removei= *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 RenFromPenn; Dec 28th, 2008 at 5:48 pm.
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;
}
#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;
}
•
•
Join Date: May 2008
Posts: 538
Reputation:
Solved Threads: 86
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.
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.
![]() |
Similar Threads
- Array of string (C)
- Problem declaring class object array (C++)
- file read and buffer problems (C++)
- Changing from reference to address (pointer?) (C++)
- changing values of a struct (C)
- what is the difference between (ptarray[]) AND &(ptarray[i]) in below example (C++)
- replace address - how to? (PHP)
- Graphics In Pixel: Part II (C++)
Other Threads in the C Forum
- Previous Thread: Challange in C
- Next Thread: Algorithm for EBCDIC to ASCII Conversion
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch calculate char cm copyanyfile copyimagefile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush fgets file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix meter microsoft motherboard mqqueue mysql number odf open opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc process program programming pyramidusingturboccodes read recursion recv repetition research scanf scheduling segmentationfault send shape socket socketprograming socketprogramming stack standard string suggestions systemcall test unix urboc user voidmain() wab win32api windows.h






