simple C question

Discussion in 'C Programming' started by Bill Cunningham, Nov 4, 2012.

  1. I have looked all over the internet for a tutorial on how to do this
    because I've forgot because I usually manually load arrays. But in such a
    case where say someone has an array with 100 elements I want to fill each
    with the numbers 0-99.
    I say 2 fors are going to be needed and 2 arrays. Then I can point an
    int somewhere to get the value of an element. This is probably simpler than
    I'm imagining. I need a refresher.

    Bill
    Bill Cunningham, Nov 4, 2012
    #1
    1. Advertising

  2. int main(void)
    {
    int a[100], i;

    for (i = 0; i < 100; i++)
    a = i;
    return 0;
    }
    Prathamesh Kulkarni, Nov 4, 2012
    #2
    1. Advertising

  3. int a[100], i;

    for (i = 0; i < 100; i++)
    a = i;
    Prathamesh Kulkarni, Nov 4, 2012
    #3
  4. Prathamesh Kulkarni wrote:
    > int main(void)
    > {
    > int a[100], i;
    >
    > for (i = 0; i < 100; i++)
    > a = i;
    > return 0;
    > }


    lol <chuckle> I see that's right. No tutorial I saw said this. It was
    i=a; or just iterating over an array without doing anything. lvaules.
    Thanks much.

    Bill
    Bill Cunningham, Nov 4, 2012
    #4
  5. On Sunday, 4 November 2012 17:09:45 UTC-5, Bill Cunningham wrote:
    > Prathamesh Kulkarni wrote:
    >
    > > int main(void)

    >
    > > {

    >
    > > int a[100], i;

    >
    > >

    >
    > > for (i = 0; i < 100; i++)

    >
    > > a = i;

    >
    > > return 0;

    >
    > > }

    >
    >
    >
    > lol <chuckle> I see that's right. No tutorial I saw said this. It was
    >
    > i=a; or just iterating over an array without doing anything. lvaules.
    >
    > Thanks much.
    >
    >
    >
    > Bill


    you could try i[a] = i as well ;)
    Prathamesh Kulkarni, Nov 4, 2012
    #5
  6. Prathamesh Kulkarni wrote:
    > On Sunday, 4 November 2012 17:09:45 UTC-5, Bill Cunningham wrote:
    >> Prathamesh Kulkarni wrote:
    >>
    >>> int main(void)

    >>
    >>> {

    >>
    >>> int a[100], i;

    >>
    >>>

    >>
    >>> for (i = 0; i < 100; i++)

    >>
    >>> a = i;

    >>
    >>> return 0;

    >>
    >>> }

    >>
    >>
    >>
    >> lol <chuckle> I see that's right. No tutorial I saw said this. It was
    >>
    >> i=a; or just iterating over an array without doing anything.
    >> lvaules.
    >>
    >> Thanks much.
    >>
    >>
    >>
    >> Bill

    >
    > you could try i[a] = i as well ;)


    This code works but it prints a seg fault right after the number 54.

    #include <stdio.h>
    #include <string.h>

    int main(void)
    {
    int a[100] = { '\0' };
    int i = 1;
    for (; i < sizeof a; i++)
    a = i;
    printf("%d\n", a[54]);
    return 0;
    }

    And it does it whether I set the entire array to 0 or not. Any guess what's
    up? And I compiled with -Wall and -ansi too.

    Bill
    Bill Cunningham, Nov 4, 2012
    #6
  7. Bill Cunningham wrote:
    > Prathamesh Kulkarni wrote:
    >> On Sunday, 4 November 2012 17:09:45 UTC-5, Bill Cunningham wrote:
    >>> Prathamesh Kulkarni wrote:
    >>>
    >>>> int main(void)
    >>>
    >>>> {
    >>>
    >>>> int a[100], i;
    >>>
    >>>>
    >>>
    >>>> for (i = 0; i < 100; i++)
    >>>
    >>>> a = i;
    >>>
    >>>> return 0;
    >>>
    >>>> }
    >>>
    >>>
    >>>
    >>> lol <chuckle> I see that's right. No tutorial I saw said this. It
    >>> was i=a; or just iterating over an array without doing anything.
    >>> lvaules.
    >>>
    >>> Thanks much.
    >>>
    >>>
    >>>
    >>> Bill

    >>
    >> you could try i[a] = i as well ;)

    >
    > This code works but it prints a seg fault right after the number
    > 54.
    > #include <stdio.h>
    > #include <string.h>
    >
    > int main(void)
    > {
    > int a[100] = { '\0' };
    > int i = 1;
    > for (; i < sizeof a; i++)
    > a = i;
    > printf("%d\n", a[54]);
    > return 0;
    > }
    >
    > And it does it whether I set the entire array to 0 or not. Any guess
    > what's up? And I compiled with -Wall and -ansi too.
    >
    > Bill


    Ok is it the string.h not being used? I forgot to take it out after removing
    memset to zero out the array and using {'\0'}

    Bill
    Bill Cunningham, Nov 4, 2012
    #7
  8. Bill Cunningham

    Ian Collins Guest

    On 11/05/12 11:39, Bill Cunningham wrote:
    >
    > This code works but it prints a seg fault right after the number 54.


    Now I'm sure you really are taking the piss.

    > #include<stdio.h>
    > #include<string.h>


    Why?

    > int main(void)
    > {
    > int a[100] = { '\0' };


    Why '\0'?

    > int i = 1;


    Why 1?

    > for (; i< sizeof a; i++)


    What is sizeof a?

    --
    Ian Collins
    Ian Collins, Nov 4, 2012
    #8
  9. Ian Collins wrote:
    > On 11/05/12 11:39, Bill Cunningham wrote:
    >>
    >> This code works but it prints a seg fault right after the
    >> number 54.

    >
    > Now I'm sure you really are taking the piss.
    >
    >> #include<stdio.h>
    >> #include<string.h>

    >
    > Why?
    >
    >> int main(void)
    >> {
    >> int a[100] = { '\0' };


    To zero out the whole array.

    > Why '\0'?


    I guess with ints it means the same as zero.

    >> int i = 1;

    >
    > Why 1?


    I want to index to 1.

    >> for (; i< sizeof a; i++)

    >
    > What is sizeof a?


    An array called a of 100 elements. I guess I could've used.

    sizeof (int)*100;

    Bill
    Bill Cunningham, Nov 4, 2012
    #9
  10. Bill Cunningham

    Ian Collins Guest

    On 11/05/12 11:50, Bill Cunningham wrote:
    > Ian Collins wrote:
    >> On 11/05/12 11:39, Bill Cunningham wrote:
    >>>
    >>> This code works but it prints a seg fault right after the
    >>> number 54.

    >>
    >> Now I'm sure you really are taking the piss.
    >>
    >>> #include<stdio.h>
    >>> #include<string.h>

    >>
    >> Why?
    >>
    >>> int main(void)
    >>> {
    >>> int a[100] = { '\0' };

    >
    > To zero out the whole array.
    >
    >> Why '\0'?

    >
    > I guess with ints it means the same as zero.


    Then why not use 0?

    >>> int i = 1;

    >>
    >> Why 1?

    >
    > I want to index to 1.


    To 1?

    >>> for (; i< sizeof a; i++)

    >>
    >> What is sizeof a?

    >
    > An array called a of 100 elements. I guess I could've used.
    >
    > sizeof (int)*100;


    Any what is the value of that expression?

    --
    Ian Collins
    Ian Collins, Nov 4, 2012
    #10
  11. On Sunday, 4 November 2012 17:50:33 UTC-5, Bill Cunningham wrote:
    > Ian Collins wrote:
    >
    > > On 11/05/12 11:39, Bill Cunningham wrote:

    >
    > >>

    >
    > >> This code works but it prints a seg fault right after the

    >
    > >> number 54.

    >
    > >

    >
    > > Now I'm sure you really are taking the piss.

    >
    > >

    >
    > >> #include<stdio.h>

    >
    > >> #include<string.h>

    >
    > >

    >
    > > Why?

    >
    > >

    >
    > >> int main(void)

    >
    > >> {

    >
    > >> int a[100] = { '\0' };

    >
    >
    >
    > To zero out the whole array.
    >
    >
    >
    > > Why '\0'?

    >
    >
    >
    > I guess with ints it means the same as zero.
    >
    >
    >
    > >> int i = 1;

    >
    > >

    >
    > > Why 1?

    >
    >
    >
    > I want to index to 1.
    >
    >
    >
    > >> for (; i< sizeof a; i++)

    >
    > >

    >
    > > What is sizeof a?

    >
    >
    >
    > An array called a of 100 elements. I guess I could've used.
    >
    >
    >
    > sizeof (int)*100;
    >
    >
    >
    > Bill


    why do you want to initalize the array with zeroes when you are filling it in the loop ?

    you are segfaulting because of the loop. you want to loop till i < 100 and the for loop terminates when i become sizeof(int) * 100
    Prathamesh Kulkarni, Nov 4, 2012
    #11
  12. Ian Collins wrote:
    > On 11/05/12 11:50, Bill Cunningham wrote:
    >> Ian Collins wrote:
    >>> On 11/05/12 11:39, Bill Cunningham wrote:
    >>>>
    >>>> This code works but it prints a seg fault right after the
    >>>> number 54.
    >>>
    >>> Now I'm sure you really are taking the piss.
    >>>
    >>>> #include<stdio.h>
    >>>> #include<string.h>
    >>>
    >>> Why?
    >>>
    >>>> int main(void)
    >>>> {
    >>>> int a[100] = { '\0' };

    >>
    >> To zero out the whole array.
    >>
    >>> Why '\0'?

    >>
    >> I guess with ints it means the same as zero.

    >
    > Then why not use 0?


    To get used to using the null character for when working with chars.

    >>>> int i = 1;
    >>>
    >>> Why 1?

    >>
    >> I want to index to 1.

    >
    > To 1?


    instead of 0 to 99 1 to 100.

    >>>> for (; i< sizeof a; i++)
    >>>
    >>> What is sizeof a?

    >>
    >> An array called a of 100 elements. I guess I could've used.
    >>
    >> sizeof (int)*100;

    >
    > Any what is the value of that expression?


    Huh. Well I guess 100 instances of however the sizeof ints are on my
    machine. I'm running a 64 bit OS.
    Bill Cunningham, Nov 4, 2012
    #12
  13. Bill Cunningham

    Ian Collins Guest

    On 11/05/12 12:00, Prathamesh Kulkarni wrote:
    >

    If you are going to use the awful google Usenet interface, please clean
    up all the extra blank lines it adds and wrap your lines to something
    sensible. Thanks.

    > why do you want to initalize the array with zeroes when you are filling it in the loop ?


    --
    Ian Collins
    Ian Collins, Nov 4, 2012
    #13
  14. Prathamesh Kulkarni wrote:

    > why do you want to initalize the array with zeroes when you are
    > filling it in the loop ?


    As habit I guess. It's better than manual initialization with 100
    elements.

    > you are segfaulting because of the loop. you want to loop till i <
    > 100 and the for loop terminates when i become sizeof(int) * 100


    SO I want

    for (i=1;i<100;i++) then. Is the {'\0'} messing up anything?

    Bill
    Bill Cunningham, Nov 4, 2012
    #14
  15. Bill Cunningham

    Ian Collins Guest

    On 11/05/12 12:09, Bill Cunningham wrote:
    > Prathamesh Kulkarni wrote:
    >
    >> why do you want to initalize the array with zeroes when you are
    >> filling it in the loop ?

    >
    > As habit I guess. It's better than manual initialization with 100
    > elements.
    >
    >> you are segfaulting because of the loop. you want to loop till i<
    >> 100 and the for loop terminates when i become sizeof(int) * 100

    >
    > SO I want
    >
    > for (i=1;i<100;i++) then. Is the {'\0'} messing up anything?


    Your head?

    --
    Ian Collins
    Ian Collins, Nov 4, 2012
    #15
  16. Ian Collins wrote:

    > Your head?


    Seems to me Ian your making a big deal about nothing. But I see what
    your saying.

    Bill
    Bill Cunningham, Nov 4, 2012
    #16
  17. Ian Collins wrote:
    > On 11/05/12 12:05, Bill Cunningham wrote:
    >> Ian Collins wrote:
    >>> On 11/05/12 11:50, Bill Cunningham wrote:
    >>>> Ian Collins wrote:
    >>>>> On 11/05/12 11:39, Bill Cunningham wrote:
    >>>>>
    >>>>>> #include<stdio.h>
    >>>>>> #include<string.h>
    >>>>>
    >>>>> Why?
    >>>>>
    >>>>>> int main(void)
    >>>>>> {
    >>>>>> int a[100] = { '\0' };
    >>>>
    >>>> To zero out the whole array.
    >>>>
    >>>>> Why '\0'?
    >>>>
    >>>> I guess with ints it means the same as zero.
    >>>
    >>> Then why not use 0?

    >>
    >> To get used to using the null character for when working with
    >> chars.

    >
    > So do you practice dog training on your cat?
    >
    >>>>>> int i = 1;
    >>>>>
    >>>>> Why 1?
    >>>>
    >>>> I want to index to 1.
    >>>
    >>> To 1?

    >>
    >> instead of 0 to 99 1 to 100.

    >
    > Given your array declaration, what is the last valid entry in your
    > array?
    >>>>>> for (; i< sizeof a; i++)
    >>>>>
    >>>>> What is sizeof a?
    >>>>
    >>>> An array called a of 100 elements. I guess I could've used.
    >>>>
    >>>> sizeof (int)*100;
    >>>
    >>> Any what is the value of that expression?

    >>
    >> Huh. Well I guess 100 instances of however the sizeof ints are on my
    >> machine. I'm running a 64 bit OS.

    >
    > So the value is?


    Not sure. Probably 16 to 32 bytes.

    > Is that a valid index to your array?


    I don't know for sure that one. Maybe this is not a case for sizeof.

    Bill
    Bill Cunningham, Nov 4, 2012
    #17
  18. Bill Cunningham

    SG Guest

    Am 04.11.2012 23:39, schrieb Bill Cunningham:
    >
    > This code works but it prints a seg fault right after the number 54.
    >
    > #include <stdio.h>
    > #include <string.h>
    >
    > int main(void)
    > {
    > int a[100] = { '\0' };
    > int i = 1;
    > for (; i < sizeof a; i++)
    > a = i;
    > printf("%d\n", a[54]);
    > return 0;
    > }
    >
    > And it does it whether I set the entire array to 0 or not. Any guess what's
    > up?


    sizeof a is not the threshold you are looking for.

    > Bill
    SG, Nov 4, 2012
    #18
  19. On Sunday, 4 November 2012 18:06:23 UTC-5, Ian Collins wrote:
    > On 11/05/12 12:00, Prathamesh Kulkarni wrote:
    >
    > >

    >
    > If you are going to use the awful google Usenet interface, please clean
    >
    > up all the extra blank lines it adds and wrap your lines to something
    >
    > sensible. Thanks.
    >
    >
    >
    > > why do you want to initalize the array with zeroes when you are filling it in the loop ?

    >
    >
    >
    > --
    >
    > Ian Collins


    I apologize for that, I am a newbie in this group.
    Would keep that in mind ;)
    Prathamesh Kulkarni, Nov 4, 2012
    #19
  20. On Sunday, 4 November 2012 18:09:30 UTC-5, Bill Cunningham wrote:
    > Prathamesh Kulkarni wrote:
    >
    >
    >
    > > why do you want to initalize the array with zeroes when you are

    >
    > > filling it in the loop ?

    >
    >
    >
    > As habit I guess. It's better than manual initialization with 100
    >
    > elements.


    No, I mean when you are going to fill
    the entire array in the loop, there's
    no point with array initialization

    >
    >
    >
    > > you are segfaulting because of the loop. you want to loop till i <

    >
    > > 100 and the for loop terminates when i become sizeof(int) * 100

    >
    >
    >
    > SO I want
    >
    >
    >
    > for (i=1;i<100;i++) then. Is the {'\0'} messing up anything?
    >
    >
    >
    > Bill


    no, the '\0' does not mess up anything,
    although 0 would be a better practice.

    It's the loop, it should be written as:
    for (i = 0; i < 100; i++)
    a = i;
    in the condition,
    i < sizeof a, you would also be using indexes
    that are out-of-bounds, hence the seg-fault
    Prathamesh Kulkarni, Nov 4, 2012
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    550
  2. Kevin Spencer

    Re: Simple Simple question!!!

    Kevin Spencer, Jun 25, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    656
    Kevin Spencer
    Jun 25, 2004
  3. Daniel Frey

    Simple Question - Simple Answer?

    Daniel Frey, Dec 28, 2004, in forum: XML
    Replies:
    4
    Views:
    835
    Daniel Frey
    Jan 12, 2005
  4. Oli

    simple simple question

    Oli, Jan 26, 2004, in forum: ASP General
    Replies:
    10
    Views:
    367
    Roland Hall
    Jan 26, 2004
  5. Peter Bailey

    simple, simple array question

    Peter Bailey, Apr 8, 2008, in forum: Ruby
    Replies:
    7
    Views:
    211
    Peter Bailey
    Apr 8, 2008
Loading...

Share This Page