Results 1 to 7 of 7

Thread: I need help with char arrays in c

  1. #1
    Senior Member
    Join Date
    Jul 2001
    Posts
    280

    I need help with char arrays in c

    I'm trying my best to learn how to program (in the small amount of free time I have) to eventually help some linux projects, so I'm writing a series of small programs just to learn the basics.

    The program I currently working on is a small password generating program. What I want is for the user to specify which consonants and vowels they would like to [potentially] have in the password. Essentially, what I need is a way to copy an array of characters to another array. The first array is a known size, but the second array can be any size, depending on the first.

    The first problem I'm having is I don't know what a character array is. Which of the following is a character array?
    Code:
    char one[] = {'a', 'e', 'i', 'o', 'u'};
    or
    Code:
    char two[] = {"aeiou"};
    My first attempt was to do something like this:
    Code:
    char one[] = {'a', 'e', 'i', 'o', 'u'};
    char *two;
    two = one;
    And that would work OK at first, but eventually [tt]two[/tt] would be appended and prepended with random characters. So I looked into the [tt]strcpy[/tt] and [tt]strxfrm[/tt] functions, but none of them seem to be working (I keep getting segfaults even though I changed [tt]two[/tt] from a pointer to an array).

    Any ideas? What am I doing wrong? Thanks for your help.

  2. #2

    Re: I need help with char arrays in c

    First, I wouldn't use str* functions as they are security threats. Look into using strl* functions instead.

    I'm not sure but two = one with two being a pointer to one can be a bit hairy. Two is refrencing the allocation of one, that might be where the problem is (not calling it correctly). It also might be that your calling beyond the length. Remember that an array of 10 starts at 0 and goes to 9 ... at 10 item would be looking into a compleatly diffrent allocation space that is probably unrelated to you.

  3. #3
    Moderator
    Advisor
    redhead's Avatar
    Join Date
    Jun 2001
    Location
    Copenhagen, Denmark
    Posts
    811

    Re: I need help with char arrays in c


    The first array is a known size, but the second array can be any size, depending on the first.
    Something like:
    Code:
    char *my_depeding_array;
    char my_first_array[] = {'a', 'b', ...., '\0'};
    
    my_depending_array = malloc(strlen(my_first_array)*sizeof(char)+ what_ever);
    strncpy(my_depeding_array, my_first_array, strlen(my_first_array));
    
    /*** Note: none of the above have been tested, just what my mind thought up ***/
    The first problem I'm having is I don't know what a character array is. Which of the following is a character array?
    Code:
    char one[] = {'a', 'e', 'i', 'o', 'u'};
    This is a char array, which is assigned to something.

    Code:
    char two[] = {"aeiou"};
    This would most likely give an error during compilation.. you assigned a char* to a given char**, which isn't compattible.

    My first attempt was to do something like this:
    Code:
    char one[] = {'a', 'e', 'i', 'o', 'u'};
    char *two;
    two = one;
    That would work too, altho the two woudl be the exact same as one, and if you were to change any char in either of them, it would also show in the other.

  4. #4
    Senior Member
    Join Date
    Jul 2001
    Posts
    280

    Re: I need help with char arrays in c


    I'm not sure but two = one with two being a pointer to one can be a bit hairy. Two is refrencing the allocation of one, that might be where the problem is (not calling it correctly). It also might be that your calling beyond the length. Remember that an array of 10 starts at 0 and goes to 9 ... at 10 item would be looking into a compleatly diffrent allocation space that is probably unrelated to you.
    What would happen is right after this call
    Code:
    char one[] = {'a', 'e', 'i', 'o', 'u'};
    char *two;
    two = one;
    [tt]two[/tt] will equal [tt]aeiou[/tt], but later (I would pass this to another function, but I do not modify the array) it would get values like [tt]salk3asdf/24\0fa4nsq/.,fgj9)u09aeiou792608761n[/tt]
    where the origional values are present, there is just extra junk before and after it.

  5. #5
    Moderator
    Advisor
    redhead's Avatar
    Join Date
    Jun 2001
    Location
    Copenhagen, Denmark
    Posts
    811

    Re: I need help with char arrays in c


    What would happen is right after this call
    Code:
    char one[] = {'a', 'e', 'i', 'o', 'u'};
    char *two;
    two = one;
    [tt]two[/tt] will equal [tt]aeiou[/tt], but later (I would pass this to another function, but I do not modify the array) it would get values like [tt]salk3asdf/24\0fa4nsq/.,fgj9)u09aeiou792608761n[/tt]
    where the origional values are present, there is just extra junk before and after it.
    This will get messy, especialy since you are parsing the pointer to another function. Lets take a walk through the scene here..

    one is a predefined array consisting of {'a', 'e', 'i', 'o', 'u'} altho it's not NULL terminated, but since we wont (or dont) change anything in it, we're not that much into it right now.
    By using char* two = one we now create a pointer to the first memory location used by one. thus the machine interpretation would be something like:
    one[0] == ('a', 0xFF00EC)
    two == 0xFF00EC
    Now this pointer is just pointing to the memory location of the letter 'a' in the char array accessed by one. At this point, when you call a function with the two as the argument, you can not be sure, what two++ will be pointing at, it could be the 0xFF00ED location in memory, or it could be the same location as the 'e' from the one array, so thats where this usage will fail, and give you some junk as the result.

  6. #6

    Re: I need help with char arrays in c

    I did a little testing on what your doing and I think you are trying to access the array as if it was a string.

    Code:
    // test.c 
    #include <stdio.h>
    
    int main(void)
    {
       char one[] = {'a','e','i','o','u'};
       char *two;
       two = one;
       printf("\n%s\n",two)
    
    }
    will result in this ....

    Code:
    [ash@gnulinux ash]$ gcc test.c 
    [ash@gnulinux ash]$ ./a.out 
    
    aeiou'6@
    Doing something like this ...
    Code:
    // test.c revised
    #include <stdio.h>
    
    int main(void)
    {
       char one[] = {'a','e','i','o','u'};
       char *two;
       two = one;
       printf("\n%c\n",two[1]);
    }
    will result in the printing of e.

  7. #7

    Re: I need help with char arrays in c

    To use it as a string use NULL termination like redhead said.

Similar Threads

  1. char pointer not work right
    By Blaqb0x in forum Linux - Software, Applications & Programming
    Replies: 5
    Last Post: 03-13-2003, 09:00 PM
  2. converting gf, special char problems
    By in forum Linux - Software, Applications & Programming
    Replies: 5
    Last Post: 06-03-2002, 07:55 AM
  3. Changing size of arrays in C++
    By Blaqb0x in forum Linux - Software, Applications & Programming
    Replies: 4
    Last Post: 05-23-2002, 07:27 AM
  4. int in char*
    By in forum Linux - Software, Applications & Programming
    Replies: 2
    Last Post: 04-12-2002, 07:24 AM
  5. What is char-major-10-135?
    By Digital_Froggy in forum Linux - General Topics
    Replies: 2
    Last Post: 12-19-2001, 01:01 PM

Bookmarks

Posting Permissions

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