| | |
Array with names
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2004
Posts: 11
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Dave Sinkula
Do you see the bug here?
C Syntax (Toggle Plain Text)
#include <stdio.h> #define N 10 int main() { int i, array[N] = {1,2,3,4,5,6,7,8,9,10}; for ( i = 1; i <= N; ++i ) { printf("array[%d] = %d\n", i, array[i]); } return 0; }
It should be like this: C Syntax (Toggle Plain Text)
for(i=1;i<=n;i++)
C Syntax (Toggle Plain Text)
for(i=1;i<=n;++i)
The second one is wrong because i increases with 1 BEFORE entering the for .. so i practically starts with the value 2. While in my example , i starts from 1 and ends at 10 , meaning exactly what it has to do.
:eek: •
•
•
•
Originally Posted by djextazy
Hehe.It should be like this:
NOTC Syntax (Toggle Plain Text)
for(i=1;i<=n;i++)
C Syntax (Toggle Plain Text)
for(i=1;i<=n;++i)
The second one is wrong because i increases with 1 BEFORE entering the for .. so i practically starts with the value 2. While in my example , i starts from 1 and ends at 10 , meaning exactly what it has to do.:eek:
And I take it that you still don't see the bug. This is exactly why you shouldn't be doing this.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Oct 2004
Posts: 11
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Dave Sinkula
No, the second one is not wrong, and your explanation is incorrect. I take it you didn't even try it.:rolleyes:
And I take it that you still don't see the bug. This is exactly why you shouldn't be doing this.
My guess is: i should start from 0? :p
•
•
•
•
Originally Posted by djextazy
Indeed , I haven't tested them until now. It seems like both versions give the same result ...
My guess is: i should start from 0? :p
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Oct 2004
Posts: 11
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by mohammad
--------------------------------------------------------------------------------
hi
look if u want array and every case on array it's name look:
#include<stdio.h>
int main()
{char *a[5]={"sami","rami","wael","dani","nami"};
for (i=0;i<5;i++)
printf("%s",a[i]);
return 0;
}
C Syntax (Toggle Plain Text)
#include <iostream> #include <conio.h> using namespace std; main() { char *a[5]; int i; for(i=0;i<5;i++) { cout<<"Dati numele #"<<i+1<<": "; cin>>a[i]; } for(i=0;i<5;i++) cout<<a[i]<<endl; getch(); }
It freezes after I input 2 names. :rolleyes: What should I do?
C Syntax (Toggle Plain Text)
char *a[5];
C Syntax (Toggle Plain Text)
char a[5][80];
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Oct 2004
Posts: 11
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Dave Sinkula
An array of uninitialized pointers. These are not automagically strings, you need to have them actually point to some memory you can write to before you try to write to it. Or actually declare an array for strings.C Syntax (Toggle Plain Text)
char *a[5];C Syntax (Toggle Plain Text)
char a[5][80];
![]() |
Similar Threads
- Array of names (C)
- Storing names in an array. (C)
- Array Names (JavaScript / DHTML / AJAX)
Other Threads in the C Forum
- Previous Thread: Loop Issues
- Next Thread: Structural Equivalence Algorithm Question
Views: 5193 | Replies: 20
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory drawing dynamic executable fflush file floatingpointvalidation fork frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide inches infiniteloop initialization interest kilometer lazy linked linkedlist linux linuxsegmentationfault list logical_drives match matrix meter microsoft motherboard multi mysql open opendocumentformat openwebfoundation pattern pause pdf pointer pointers posix power problem program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming spoonfeeding stack standard strchr string strings structures student suggestions system test testautomation unix urboc user voidmain() win32 win32api windows.h






