How to concatenate Matrix's elements ?

Discussion in 'C Programming' started by Nezhate, Jul 21, 2008.

  1. Nezhate

    Nezhate Guest

    Hi all,
    Is there any way to print a matrix's elements using a simple printf ?
    what I want as result is for example:
    if mat ={0,1,2,3}
    result must be: "0123".

    I tried this code:

    /***************************************************************/
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #define size 10

    int main (void)
    {
    // Declaration
    int i = 0;
    int mat[size]= {0,0,0,0};
    char convert[size]= {0,0,0,0};

    for (i=0;i<4;i++)
    {
    mat=mat+i+1;
    printf("mat[%d]=%d\n",i,mat);

    // Convert int to string
    sprintf(convert,"%s",(char *)&mat);
    // concatenate matrix's elements
    // strcat(convert[i+1],convert);

    }

    printf ("Matrix is equal to %s\n",(char *)&convert);

    return (0);
    }

    /***************************************************************/

    After execution I get this
    $ ./matrix
    mat[0]=1
    mat[1]=2
    mat[2]=3
    mat[3]=4
    Matrix is equal to 

    If I activate the line :
    strcat(convert[i+1],convert);
    I get
    matrix.c: In function ‘main’:
    matrix.c:23: warning: passing argument 1 of ‘strcat’ makes pointer
    from integer without a cast
    matrix.c:23: warning: passing argument 2 of ‘strcat’ makes pointer
    from integer without a cast

    And when executing :
    ./matrix
    mat[0]=1
    Segmentation fault.
     
    Nezhate, Jul 21, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lvcargnini

    Matrix composed by two matrix

    lvcargnini, Jul 4, 2006, in forum: VHDL
    Replies:
    3
    Views:
    2,675
    Jonathan Bromley
    Jul 5, 2006
  2. Holgerson

    Matrix*Vector and Vector*Matrix

    Holgerson, Oct 25, 2007, in forum: C++
    Replies:
    3
    Views:
    409
    Holgerson
    Oct 26, 2007
  3. antar2
    Replies:
    3
    Views:
    370
    Niklas Norrthon
    Jul 23, 2008
  4. Terry Reedy
    Replies:
    0
    Views:
    557
    Terry Reedy
    Apr 2, 2009
  5. Carlos

    Concatenate/De-Concatenate

    Carlos, Oct 12, 2012, in forum: VHDL
    Replies:
    10
    Views:
    892
Loading...

Share This Page