Array with names

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2004
Posts: 11
Reputation: djextazy is an unknown quantity at this point 
Solved Threads: 0
djextazy djextazy is offline Offline
Newbie Poster

Re: Array with names

 
0
  #11
Oct 6th, 2004
Originally Posted by Dave Sinkula
Do you see the bug here?
  1. #include <stdio.h>
  2.  
  3. #define N 10
  4.  
  5. int main()
  6. {
  7. int i, array[N] = {1,2,3,4,5,6,7,8,9,10};
  8. for ( i = 1; i <= N; ++i )
  9. {
  10. printf("array[%d] = %d\n", i, array[i]);
  11. }
  12. return 0;
  13. }
Hehe. It should be like this:

  1. for(i=1;i<=n;i++)
NOT
  1. 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:
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Array with names

 
0
  #12
Oct 6th, 2004
Originally Posted by djextazy
Hehe. It should be like this:

  1. for(i=1;i<=n;i++)
NOT
  1. 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:
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.
"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
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 11
Reputation: djextazy is an unknown quantity at this point 
Solved Threads: 0
djextazy djextazy is offline Offline
Newbie Poster

Re: Array with names

 
0
  #13
Oct 6th, 2004
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.
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Array with names

 
0
  #14
Oct 6th, 2004
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
Well, yes, but the bug is that you should not attempt to access array[10] because it is beyond the array bounds.
"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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Array with names

 
0
  #15
Oct 6th, 2004
>My guess is: i should start from 0?
If you have to guess, don't be so quick to answer questions because you'll probably be wrong. Even if you bother to test your assumptions and they pan out, you could still be wrong on something more subtle.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 5
Reputation: mohammad is an unknown quantity at this point 
Solved Threads: 0
mohammad mohammad is offline Offline
Newbie Poster

Re: Array with names

 
0
  #16
Oct 8th, 2004
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;
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 5
Reputation: mohammad is an unknown quantity at this point 
Solved Threads: 0
mohammad mohammad is offline Offline
Newbie Poster

Re: Array with names

 
0
  #17
Oct 8th, 2004
--------------------------------------------------------------------------------
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;
}
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 11
Reputation: djextazy is an unknown quantity at this point 
Solved Threads: 0
djextazy djextazy is offline Offline
Newbie Poster

Re: Array with names

 
0
  #18
Oct 8th, 2004
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;
}
I want it with #include <iostream> and all the rest ( cout & cin) . This one doesn't work:

  1. #include <iostream>
  2. #include <conio.h>
  3. using namespace std;
  4. main()
  5. { char *a[5]; int i;
  6. for(i=0;i<5;i++) { cout<<"Dati numele #"<<i+1<<": "; cin>>a[i]; }
  7. for(i=0;i<5;i++) cout<<a[i]<<endl;
  8. getch();
  9. }

It freezes after I input 2 names. :rolleyes: What should I do?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Array with names

 
0
  #19
Oct 8th, 2004
  1. char *a[5];
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.
  1. 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
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 11
Reputation: djextazy is an unknown quantity at this point 
Solved Threads: 0
djextazy djextazy is offline Offline
Newbie Poster

Re: Array with names

 
0
  #20
Oct 8th, 2004
Originally Posted by Dave Sinkula
  1. char *a[5];
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.
  1. char a[5][80];
I actually worked out the exercise with a double array , as you said ( a[5][80 ). But I was just churious about mohammad's version . :rolleyes:
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum


Views: 5193 | Replies: 20
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC