| | |
Array with names
![]() |
•
•
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
| Thread Tools | Search this Thread |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue mysql number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






