943,502 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 7504
  • C RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Oct 6th, 2004
0

Re: Array with names

Quote 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:
Reputation Points: 10
Solved Threads: 0
Newbie Poster
djextazy is offline Offline
11 posts
since Oct 2004
Oct 6th, 2004
0

Re: Array with names

Quote 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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Oct 6th, 2004
0

Re: Array with names

Quote 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
djextazy is offline Offline
11 posts
since Oct 2004
Oct 6th, 2004
0

Re: Array with names

Quote 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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Oct 6th, 2004
0

Re: Array with names

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 8th, 2004
0

Re: Array with names

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;
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mohammad is offline Offline
5 posts
since Sep 2004
Oct 8th, 2004
0

Re: Array with names

--------------------------------------------------------------------------------
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;
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mohammad is offline Offline
5 posts
since Sep 2004
Oct 8th, 2004
0

Re: Array with names

Quote 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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
djextazy is offline Offline
11 posts
since Oct 2004
Oct 8th, 2004
0

Re: Array with names

  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];
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Oct 8th, 2004
0

Re: Array with names

Quote 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:
Reputation Points: 10
Solved Threads: 0
Newbie Poster
djextazy is offline Offline
11 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: hello world
Next Thread in C Forum Timeline: help me out on opencv





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC