Two Dimensional Array Of Characters

This is a discussion on Two Dimensional Array Of Characters within the C Programming forums, part of the General Programming Boards category; Hi, I am looking for someway to read an input from the user, and put it into a two dimensional ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    17

    Two Dimensional Array Of Characters

    Hi, I am looking for someway to read an input from the user, and put it into a two dimensional array.

    For project purposes, my function looks something like this, with declarations of the arrays in the main:
    Code:
    main{ 
    char arrayA = [5][9];
    int arrayB = [5];
    }
    
    int info(char *arrayA [][], int *arrayB[])
    {
                printf("Enter code here:  ");
            ... need to get input from user here
    }
    Basically I need, 5 items with a maximum length of 8 characters ([9] was declared for the '\0')

    Any help would be great.
    Thanks.

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    You could just loop through the input and store it at the approriate place. Are there any more restrictions ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    17
    no more restriictions, I'm just saying, I don't know how to get the input from the user and store it in the 2D array. i tried fgets but because of my pointer it wont work.

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    You could create a buffer to read your desired 8 character string, and then you could put it in the appropriate place using strcpy.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  5. #5
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Code:
    int info(char *arrayA [][], int *arrayB[])
    This line is incorrect. When sending multi-dim arrays to functions, you need to specify the number of elements in all dimensions except the first ( if sending as an arrray ) - char arrayA[][9]. Notice also, that you don't need to send it as a pointer (although that is also an option ); send it as a pointer, or as an array.

    So the same must be done for arrayB - int arrayB[]; this could also be done using int *arrayB

    NB. I think for a multi-dim array, the pointer notation would be char **arrayA, but I'm not sure.

    As for getting the input from the user, a loop using fgets, a buffer and strcpy is going to be the best way to go.

    Some good reading:
    http://faq.cprogramming.com/cgi-bin/...&id=1073086407
    http://www.cprogramming.com/tutorial/c/lesson8.html
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    17
    well what I was going for, was getting two return values with the
    Code:
    int info(char *arrayA [][], int *arrayB[])

  7. #7
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    By sending the array as I showed before, you can alter the original.

    This code sends the address of each array to the function:
    Code:
    int info(char arrayA [][9], int arrayB[])
    Arrays are not pointers, but they have some similarities. For example, the address of an array using pointer notation, is the same as the address of element [0].

    *arrayA == &arrayA[0]

    Have a read of those links I gave you, it should clear it up a little more.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    > int info(char *arrayA [][], int *arrayB[])
    Would be
    int info(char arrayA[][9], int arrayB[])

    And main would call it with
    info( arrayA, arrayB );

    Anything you wrote to the arrays in info() would be visible in main.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Woot, for once I got something right. :B

    Salem, would char **arrayA be the pointer equivalent of char array[][x]?
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    17
    Quote Originally Posted by bivhitscar
    By sending the array as I showed before, you can alter the original.

    This code sends the address of each array to the function:
    Code:
    int info(char arrayA [][9], int arrayB[])
    Arrays are not pointers, but they have some similarities. For example, the address of an array using pointer notation, is the same as the address of element [0].

    *arrayA == &arrayA[0]

    Have a read of those links I gave you, it should clear it up a little more.
    no that's incorrect.
    arrayA == &arrayA[0].
    also, *(arrayA + 1) == arrayA[1];

    An array is just a pointer to the first element in the array. So *arrayA is the first element.

  11. #11
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Oops, sorry bout that.

    I was thinking of this in my head: *arrayA == &arrayA[0][0]

    Is that right? *scratches head*
    Last edited by bivhitscar; 05-24-2006 at 01:36 AM.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  12. #12
    Registered User
    Join Date
    May 2006
    Posts
    17
    no that's wrong as well.
    arrayA[0][0] gives the first element, so &arrayA[0][0] just gives the address of the first element.
    *arrayA dereferences the address arrayA points to, which is the first element.

  13. #13
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Quote Originally Posted by jarro_2783
    no that's wrong as well.
    arrayA[0][0] gives the first element, so &arrayA[0][0] just gives the address of the first element.
    *arrayA dereferences the address arrayA points to, which is the first element.
    Nuh-uh. Dereferencing arrayA once will give the value of arrayA[0] - which, in our case, is the address of arrayA[0][0], or &arrayA[0][0].

    If we dereference twice, **arrayA, this will yeild the value at arrayA[0][0].

    *arrayA == &arrayA[0][0]
    **arrayA == arrayA[0][0]
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  14. #14
    Registered User
    Join Date
    May 2006
    Posts
    17
    oh yeah, you're right.

    An interesting thing to note though, because of the way arrays are stored you get this:

    Code:
    #include <stdio.h>
    
    int main(int argc, char **argv) {
    
    	int array[5][5] = {{1}};
    
    	printf("&array: %d\n", array);
    	printf("*array: %d\n", *array);
    	printf("**array: %d\n", **array);
    	printf("array[0][0]: %d\n", array[0][0]);
    	printf("&array[0][0]: %d\n", &array[0][0]);
    	printf("&array[0]: %d\n", &array[0][0]);
    
    	return 0;
    }
    outputs:
    Code:
    &array: 2293504
    *array: 2293504
    **array: 1
    array[0][0]: 1
    &array[0][0]: 2293504
    &array[0]: 2293504
    so the addresses are actually the same, and dereferencing once still gives the same address.

    Although if you passed a 2d array as a pointer instead of array[][size] c wouldn't know the difference, and you would only have to dereference once to access the first element.
    You would however have to do the calculations yourself to get to the elements, you couldn't just put array[1][4];

  15. #15
    Registered User
    Join Date
    May 2006
    Posts
    17
    anyways...
    tried to do a loop through with fgets and I got this error:
    [code]
    if(fgets(arrayA, sizeof(arrayA), stdin) != NULL)

    Type error in argument 1 to 'fgets'; found 'char [9] *' expected 'restrict char *'.

    [\code]

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Zero out two dimensional array
    By davo666 in forum C Programming
    Replies: 16
    Last Post: 01-08-2009, 04:28 AM
  2. Multi dimensional array
    By $l4xklynx in forum C Programming
    Replies: 7
    Last Post: 01-03-2009, 02:56 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. 2 dimensional char array question
    By knutso in forum C++ Programming
    Replies: 4
    Last Post: 12-05-2002, 05:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21