Thread: Two dimensional char array

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    23

    Two dimensional char array

    Here is what I basically need:

    I need to be able to take in say 2 words on 10 lines
    each word can be a maximum of 30 chars

    heres what I have but it seems incorrect:

    Code:
    char **c2names;
    c2names = (char **) malloc(amount_of_lines * sizeof(char *));
    	for(i = 0; i < amount_of_lines; i++)
    	{
    		c2names[i] =(char *) malloc(30 * sizeof(char));
    	}

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Your memory allocation looks correct.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    23
    correct way to call it would be
    Code:
    scanf("%s %s", c2names[0][0], c2names[0][0]);
    ?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Correct way to do what? To scan a string into a single "line", you'd do:
    Code:
    scanf( "%s", c2names[ line ] );
    Assuming you actually wanted to use scanf...


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    23
    yes but what if I want to take in 2 words

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Can't you think for yourself at all? I don't see your attempt to do anything here. Where is it? While you're posting it, how about posting what you think you're doing, so we actually know what it is you're trying? Give us a line by line of what you think is happening, so we can tell you if you're right or not.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    23
    Well heres what is going on in my head,

    I want to take in say 10 lines and 2 words per line (seperate)

    so
    Code:
    char **c2names;
    
    c2names = (char **) malloc(amount_of_lines * sizeof(char *));
    
    for(i = 0; i < amount_of_lines; i++)
    {
    	c2names[i] =(char *) malloc(30 * sizeof(char));
    }
    
    int i;
    for(i = 0; i < amount_of_lines; i++)
    {
    fscanf(fptr, "%s %s", c2names[i]);
    }
    sorry for even posting this question (I program mainly java NEVER C) =P
    Last edited by eurus; 01-18-2006 at 11:09 PM.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    scanf isn't really very good for reading strings. I mean, you can make it work with some bracket trickery, but you'd be much better off just looking at the link Dave provided above, and using fgets.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by eurus
    Well heres what is going on in my head,
    Why not post the actual input and expected output rather than a perceived solution to an undescribed problem? The scanf family is not for the inexperienced.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Registered User
    Join Date
    Jan 2006
    Posts
    23
    ok

    input via file
    **
    2 2
    Hi there
    my name
    **

    2 and 2 is the numbers that signify 2 rows. 2 columns

    Code:
    char **c2names;
    
    int amount_of_lines, words;
    
    fscanf("%d %d", &amount_of_lines, &words);
    
    c2names = (char **) malloc(amount_of_lines * sizeof(char *));
    
    for(i = 0; i < amount_of_lines; i++)
    {
    	c2names[i] =(char *) malloc(30 * sizeof(char));
    }
    
    int i;
    for(i = 0; i < amount_of_lines; i++)
    {
    fscanf(fptr, "%s %s", c2names[i]);
    }

    is no output it should just store the hi there in one row of array and my name in second row where each word gets its own element

    making each word accessable through array calling.

    Now the question is do I use the int words in any of the malloc and am If I could access each element in fscanf am I doing it correctly.
    Last edited by eurus; 01-18-2006 at 11:24 PM.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you simply take the time to look at your code and think about what you're doing, you can see it's wrong.
    Code:
    fscanf(fptr, "%s %s", c2names[i]);
    You have two scan codes in your fscanf statement, but you only provide 1 argument. Or...
    Code:
    scanf("%s %s", c2names[0][0], c2names[0][0]);
    You actually do provide two arguments, but they're to the exact same spot. Thus, one will overwrite the other. (Well, if it wasn't incorrectly done it would have.)

    Try compiling your code, rather than just posting in something random. Compile with warnings enabled.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Jan 2006
    Posts
    23
    with the [0][0] though I get an error, only works with
    Code:
    scanf("%s %s", c2names[0]);
    and that was a typo for the post should be
    Code:
    scanf("%s %s", c2names[0][0], c2names[0][1]);
    I compile on a sep computer sorry =P

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Do you know why it doesn't work with [0][0]? If you don't understand, take the time to. It will help you out in the long run. Don't just try to kludge code together to get the "correct" output. Try to actually understand why it does or doesn't do what it does.

    [0][0] doesn't work because that is the location of an actual single character in your "array". You're trying to read a string, which is a series of characters. Thus, you don't specify a single character to read it to. (It was wrong anyway, because were you trying to read into a single character, you would have needed to provied the address of the single character. Not the character itself.)

    It works when you just provide [0], because that's referring to one line in your two dimensional array. It works out like so:
    Code:
    char **twodee;
    
    twodee = malloc( numoflines * sizeof( char * ) ); /* allocate a bunch of lines. */
    for( thisline = 0; thisline < numoflines; thisline++ )
        twodee[ thisline ] = malloc( lengthofline );
    Now, just like you allocate each line, you read into the single line the same way. If you want to read into a character (one at a time) in a specific line, then you provide the line and character you want to read into:
    Code:
    twodee[ line ][ character ] = 'a';
    Like so.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-17-2008, 12:36 PM
  2. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM