| | |
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
| Thread Tools | Search this Thread |
#include * append array arrays bash binarysearch changingto char character cm copyanyfile copyimagefile copypdffile creafecopyofanytypeoffileinc createprocess() database dynamic execv feet fgets file floatingpointvalidation fork forloop framework function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram homework i/o ide include infiniteloop initialization input interest intmain() iso kernel keyboard kilometer license linked linkedlist linux list lists looping loopinsideloop. lowest matrix meter microsoft mqqueue oddnumber odf open openwebfoundation overwrite pdf pointer pointers posix power process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing segmentationfault sequential single socket socketprogramming standard strchr string suggestions test testing threads turboc unix urboc user whythiscodecausesegmentationfault win32api windowsapi






