Results 1 to 3 of 3

Thread: Filling an array with a string

  1. #1
    johnerr
    Guest

    Filling an array with a string

    How would one go about putting the characters of a sting into the index of an array?
    eg.
    For i = 1 To 8
    array(i) = "abcdefgh"
    Next
    I want array(1) to be a , array(2) to be b and so on.
    Thanks

  2. #2
    jim mcnamara
    Guest
    Code:
    memset(arr,'\0',sizeof(arr));  // zero out the array
    for (i=1;i<9;i++){
        arr[i-1] = i;  
    }
    The problem is: arrays start counting with zero, and zero is used by C to mark the end of a string so:
    Code:
    arr[0]=0;
    will create an array with no length - like a null string in VB.
    So,
    Code:
    arr[0]=' ';
    or something like that for the first element

  3. #3
    jim mcnamara
    Guest
    or this
    Code:
    char c;
    int i=0;
    for (c='a';c<='h';c++){
         arr[i++]=c;
    }
    my problem is I have no idea what you want....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width