Home > Software engineering >  In C, is 'int *' a derived datatype?
In C, is 'int *' a derived datatype?

Time:10-31

Is int * a derived datatype or not?

I got this confusion because I feel that these two cases are contradicting in regard to this question.


case 1: Assumption: int * is to be considered as a derived datatype. then considering this below given code -

void someRandomFunction()
{
    int* a, b, c, d;
    int  e, f, g, h;
    ....
    ....
    ....
}

In this function, if we need to consider int * as a derived datatype, then just like e, f, g, h are variables of int datatype, all the variables a, b, c, d must be pointers pointing to int datatype, right?

But since it is only a which is a pointer pointing to int datatype, so does this disprove our assumption of this case?


Case 2: Assumption: int * is not to be considered as a derived datatype. then considering this below given code -

int* MyFunc()
{
    int *p;
    ....
    ....
    ....
    return p;
}

Here, int * is instructing the return's datatype to the compiler, right? So, Does this prove that int * is a derived datatype, i.e., does this disprove our assumption of this case?

CodePudding user response:

Is int * a derived datatype or not?

It is a derived type from the object type int.

if we need to consider int * as a derived datatype, then just like e, f, g, h are variables of int datatype, all the variables a, b, c, d must be pointers pointing to int datatype, right?

If you will introduce the type int * as a type specifier as for example

typedef int * T;

then indeed in this declaration

T a, b, c, d;

all the declared variables have the type int *. Otherwise the symbol * is related to the declarator of the variable a in this declaration

int* a, b, c, d;

that may be rewritten like

int ( * a ), b, c, d;

Pay attention to that declaration is defined like (here is a partial definition of declaration):

declaration:
    declaration-specifiers init-declarator-listopt ;

declaration-specifiers:
    type-qualifier declaration-specifiersopt

init-declarator-list:
    init-declarator
    init-declarator-list , init-declarator

init-declarator:
    declarator
    declarator = initializer

That is in this declaration

int* a, b, c, d;

the common type specifier of all declarators is int and the declarator a has the form *a.

While in this declaration

T a, b, c, d;

the common type specifier is T that represents the derived pointer type int *.

That is the derived pointer type int * is being built from its referenced type int. The derived type int ** is being built from its referenced type int * and so on.

So for example passing by reference in C means passing an object indirectly through another object of its derived pointer type that references the type of the original object.

CodePudding user response:

Ok. We have an fundamental datatypes (int, float, double,...) and we have an derived datatypes and we also have a derived datatypes(fundamental datatype with some extensions).

In your first example int* a, b, c, d; we have an a as a pointer to integer but b, c, d are not pointers to the integers, because they are just integers !

So, yes you are right! int* is derived from the int. Your confusion has started with taking b,c,d also as a pointers but they are not. In C programming language during the variable declaration process if there is an * between variable name and hers datatype than that variable is a pointer to hers datatype.

CodePudding user response:

A derived datatype is a datatype derived from a fundamental datatype. In this case, int * is a derived datatype from int.

Your first analysis is incorrect. Getting b, c, d to have int datatype doesn't say that int * is not a derived datatype. So It's just about how C is working. that's why it's always a good idea to write the declaration like that: int *a, .... so the * is adjacent to the variable, not the type to signalize that any coming variables don't take the * but only the datatype.

Just like the array.

int a[10], b;

here the array is a derived datatype but b has type int doesn't also suggest the array is not a derived datatype.

CodePudding user response:

Arrays, structures, unions, functions, pointers, and atomic types are derived from other types.

int* a, b; is not a way of expressing that b is a pointer type. The grammar of the declaration is such that * binds with a; the declaration is actually int *a, b;, as if it were int *a; int b;.

In C, declarations use a basic type, like int, and then describe derived types by using a “picture” of how the type will be used. In int *a;, we are saying that *a will be used as an int. From this, it is deduced that a must be a pointer to an int. Similarly, int *a[3] says that *a[i] will be used as an int, so a[i] must be a pointer to an int, so a must be an array of pointers to int.

Then int *a, b; says *a is an int and b is an int.

  • Related