Thread: How to strcat in 2D - array?

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    6

    How to strcat in 2D - array?

    How do I actually strcat in 2D-array in general method or as shown in the code below:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    void twoDarray( char(*ptr)[4] , int n );


    void main()
    {
    char array[3][4]={"helo", "piec", "appl"};
    char (*ptr)[4];

    ptr = array;

    twoDarray( ptr , 3 );
    }

    void twoDarray( char(*ptr)[4] , int n )
    {
    char str2[1][2];
    int a, b;

    str2[1][1] = '\0';

    for( a = 0 ; a < n ; a++ )
    {
    for( b = 93 ; b < 123 ; b++ )
    {
    str2[1][0] = b;
    strcat( str2[1][0] , ptr++ );
    puts( str2[1] );
    }
    }
    }

    I have no idea what I'm doing?
    Please aid me!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well if you have a 1D array
    char str[100];

    Then you could do this
    strcat( str, "hello" );


    For a 2D array,
    char str[5][100];

    Then you could do this
    strcat( str[0], "hello" );
    strcat( str[1], "world" );

    > for( b = 93 ; b < 123 ; b++ )
    Is this the same as
    for( b = 'a' ; b < 'z' ; b++ )

    Almost all your arrays in your program have array overrun problems - it's hard to say what's happening, or what you're trying to achieve.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  2. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  3. cannot print out my 2d array correctly! please help
    By dalearyous in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2006, 02:07 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