0

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?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 4
    What do you mean by derived datatype? – kiner_shah Oct 30 '21 at 09:23
  • b,c,d,e,f,g,h are not pointers to int. The identifier 'b' for example is a direct translation to the adress of the int variable. While the identifier 'a' is a direct translation to the adress which holds an adress to an int variable. A subtle difference. – Fredrik Oct 30 '21 at 09:25
  • Any property of `b` does not mean anything whether `int*` is a derived data type or not (whatever you mean by that) as `b` is not of type `int` at all. You cannot deduct any property about X from objects that are not X. – Gerhardh Oct 30 '21 at 09:28
  • I have a hard time understanding how you reason. I was almost with you all the way when you reasoned about "yes, it's a derived datatype" but you lost me at the end. – Ted Lyngmo Oct 30 '21 at 09:31
  • I never heard about the concept of derived data types but a simple google search says all pointers are derived data types https://www.onlinetutorialspoint.com/c-program/c-derived-and-user-defined-data-types.html – Sulthan Oct 30 '21 at 09:35
  • 3
    Does this answer your question? [C and derived data types?](https://stackoverflow.com/questions/6025465/c-and-derived-data-types) – Sulthan Oct 30 '21 at 09:36
  • @kiner_shah I just saw an answer where someone has mentioned that there are 3 types of datatypes in c - Basic, Derived and user-defined. I'm not 100 percent sure about their definitions so I don't know whether I can ask whether 'int *' is a **derived datatype** or not. But is 'int *' still a datatype (no matter which classification does it belongs to among the Basic, Derived and User-defined)? –  Oct 30 '21 at 09:40
  • As per [this document](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf), in section 6.2.5 point number 20, a pointer type is a derived type. `int*` is a pointer to `int` so, yeah it seems like it is a derived type. – kiner_shah Oct 30 '21 at 09:51

4 Answers4

1

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

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.

0

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.

Alaa Mahran
  • 663
  • 4
  • 12
0

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.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312