matrix - Simple c prog. error: invalid type arguement of unary '*' -
this first post here , i'm new c.
i want write program able print matrixes. should like:
---- -o-- ooo- ----
so want printed beginning.
my current code is:
// 4. exercise // learn 2d arrays #include <stdio.h> char z; char s; char m1_ptr; void creatematrix() { for(z = 0; z != 4; z++) { (s = 0; s != 4; s++) { printf("%c", *m1_ptr); } printf("\n"); } } //------------------------------------------------------------------ int main() { char o = o; char c = '-'; // , variables matrix count: char matrix_1 [4][4] ={{c,c,c,c},{c,o,c,c},{o,o,o,c},{c,c,c,c}}; char *m1_ptr = &matrix_1 [z][s]; creatematrix(matrix_1 [0][0]); /* for(z = 0; z != 4; z++) { (s = 0; s != 4; s++) { printf("%c", matrix_1 [z][s]); } printf("\n"); } */ return 0; }
it works if put void function main, since there more matrixes coming want in function make more readable.
if compile error message:
"line17: error: invalid type argument of unary '*' ('have int')" (edit: line 17 says "printf("c......")
i looked @ other questions, since understand super simple programs yet didn't work out me.
does know how fixed? (it nice if answer explains why, since have little experience pointers)
i think looking this:
#include <stdio.h> #define row 4 #define column 4 void printmatrix(int rowlength, int columnlength, char matrix[rowlength][columnlength]) { int rowcount, columncount; for(rowcount = 0; rowcount < rowlength; rowcount++) { for(columncount = 0; columncount < columnlength; columncount++) printf("%c", matrix[rowcount][columncount]); printf("\n"); } } int main() { char o = 'o'; char c = '-'; char matrix_1 [row][column] = { {c,c,c,c}, {c,o,c,c}, {o,o,o,c}, {c,c,c,c} }; printmatrix(row, column, matrix_1); return 0; }
it prints pattern want
Comments
Post a Comment