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.
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.