Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can anyone help me to understand typedef in this program?

Tags:

c

typedef

its a simple C program only thing I don't understand in this one is: when we write

typedef int RowArray[COLS];    

The way I thought typedef works was everything from typedef until the last word in gets replaced by the last word before ";". So in this there must a something like typedef int RowArray[COLS] X; then X *rptr;

But here I am unable to understand it. If possible can you send me a link to some material on typedef where this kind of situation is explained.

#include <stdio.h>
#include <stdlib.h>
#define COLS 5

typedef int RowArray[COLS];  // use of typedef 
RowArray *rptr;              // here dont we have to do 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");
    }
  return 0;
}

explanation of typedef From above link I understood the working of typedef in the example code I posted in the problem. But reading a little further in the link it shows another example of typedef.

//To re-use a name already declared as a typedef, its declaration must include at least one type specifier, which removes any ambiguity:

typedef int new_thing;
func(new_thing x){
        float new_thing;
        new_thing = x;
}

Now if someone can explain what happened here as it makes it more confusing for me. As in first line

typedef int new_thing;

func(new_thing x)    //here i assume it works as (int x) as we did typedef int new_thing earlier.

but when the braces starts

{
    float new_thing; //so what happens here exactly (float int;) ???
    new_thing = x;   // and here too int = x; ????
}

its obvious I am missing something and interpreting it wrongly. Thanks for help.

like image 732
Deepak Avatar asked Dec 26 '22 07:12

Deepak


2 Answers

You are confusing typedef with #define. The preprocessor #define is the one that does simply text replacing.

typedef, on the other hand, is not part of preprocessor, but syntactically like keyword extern, static, etc. It gives a certain type a new name.

typedef int RowArray[COLS]; 

RowArray defines a type of an int array with COLS elements. So

RowArray *rptr; 

rptr is a pointer to an int array with COLS elements.

like image 125
Yu Hao Avatar answered Dec 27 '22 20:12

Yu Hao


Although typedef is thought of as being a storage class, it isn't really. It allows you to introduce synonyms for types which could have been declared some other way. The new name becomes equivalent to the type that you wanted, as this example shows.

typedef int aaa, bbb, ccc;
typedef int ar[15], arr[9][6];
typedef char c, *cp, carr[100];

/* now declare some objects */

/* all ints */
aaa     int1;
bbb     int2;
ccc     int3;

ar      yyy;    /* array of 15 ints */
arr     xxx;    /* 9*6 array of int */

c       ch;     /* a char */
cp      pnt;    /* pointer to char */
carr    chry;   /* array of 100 char */

The general rule with the use of typedef is to write out a declaration as if you were declaring variables of the types that you want. Where a declaration would have introduced names with particular types, prefixing the whole thing with typedef means that, instead of getting variables declared, you declare new type names instead. Those new type names can then be used as the prefix to the declaration of variables of the new type.

like image 44
Ruslan Gerasimov Avatar answered Dec 27 '22 19:12

Ruslan Gerasimov