c - Can anyone help me to understand typedef in this program? -
its simple c programme thing don't understand in 1 is: when write
typedef int rowarray[cols];
the way thought typedef works typedef until lastly word in gets replaced lastly word before ";". in there must typedef int rowarray[cols] x; x *rptr;
but here unable understand it. if possible can send me link material on typedef kind of situation explained.
#include <stdio.h> #include <stdlib.h> #define cols 5 typedef int rowarray[cols]; // utilize of typedef rowarray *rptr; // here dont have rowarray[cols] *rptr; int main() { int nrows = 10; int row, col; rptr = malloc(nrows * cols * sizeof(int)); for(row=0; row < nrows; row++) { for(col=0; col < cols; col++) { rptr[row][col] = 17; } } for(row=0; row<nrows; row++) { for(col=0; col<cols; col++) { printf("%d ", rptr[row][col]); } printf("\n"); } homecoming 0; }
explanation of typedef above link understood working of typedef in illustration code posted in problem. reading little farther in link shows illustration of typedef.
//to re-use name declared typedef, declaration must include @ to the lowest degree 1 type specifier, removes ambiguity: typedef int new_thing; func(new_thing x){ float new_thing; new_thing = x; }
now if can explain happened here makes more confusing me. in first line
typedef int new_thing; func(new_thing x) //here assume works (int x) did typedef int new_thing earlier.
but when braces starts
{ float new_thing; //so happens here (float int;) ??? new_thing = x; // , here int = x; ???? }
its obvious missing , interpreting wrongly. help.
you confusing typedef
#define
. preprocessor #define
1 text replacing.
typedef
, on other hand, not part of preprocessor, syntactically keyword extern
, static
, etc. gives type new name.
typedef int rowarray[cols];
rowarray
defines type of int
array cols
elements. so
rowarray *rptr;
rptr
pointer int
array cols
elements.
c typedef
No comments:
Post a Comment