How to strcat in 2D - array?

This is a discussion on How to strcat in 2D - array? within the C Programming forums, part of the General Programming Boards category; How do I actually strcat in 2D-array in general method or as shown in the code below: #include <stdio.h> #include ...

  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
    32,821
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 04:45 AM
  2. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 09: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, 03:07 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 01:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM

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