Thread: New with 2D array

  1. #1
    Unregistered
    Guest

    New with 2D array

    if i have 2D array char info [2][8];
    [2 9 0 5 J O H N]
    [3 5 5 2 1 i a n ]
    if i want to show [0][1]
    it should show '9'

    i tried this one puts(info[0][1]);
    it didn't seem to work

    any idea?
    cheers

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    This should work: printf ("%c\n", info [0][1]);

  3. #3
    Unregistered
    Guest
    thanks
    how can i copy it to another variable
    i used like this but doesn't seem to be working

    char temp[4];

    for i=0;i<4;i++
    strcpy(temp,info[0][i]);

    should put [i] after temp?
    i also tried temp[i]=info[0][i]; the value doesn't go through i think

    any suggestion?
    cheers mate

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: New with 2D array

    Originally posted by Unregistered
    if i have 2D array char info [2][8];
    [2 9 0 5 J O H N]
    [3 5 5 2 1 i a n ]
    Just a small point...... If you are using these array as strings, you need to create the rows 1 longer than the string itself to allow for the NULL terminator.

    As for copying parts of a string, you can do this a few ways. One is to use strncpy(), which allows you to copy upto n characters from one char array to another (just watch out the the null terminator that isn't necessary appended to the target). Or you can make a bit of code to do the copy yourself. Something like
    Code:
    for (i = 0; i < 4; i++)
    	target[i] = source[i];
    Again, you will need to take care of NULL terminator if you intend to use these as strings.

    [EDIT] In your case, the source array in my sample code would be coded like this: source[0][i]
    The 0 would be replaced with the relevant index number for the string you want to work with. Here's a working example:
    Code:
    #include <stdio.h>
    int main(void)
    {
    	int i;
    	
    	char *source[] = {"abcdefgh", "ijklmnop"};
    	char target[100];
    	
    	for (i = 0; i < 4; i++)
    		target[i] = source[0][i];
    	target[i] = '\0';
    	
    	printf("source[0]: %s\n", source[0]);
    	printf("target: %s", target);
    	
    	return (0);
    }
    Last edited by Hammer; 05-04-2002 at 06:51 PM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  3. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM