How to concatenate Matrix's elements ?

N

Nezhate

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top