Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
const and typedef of arrays in C

In C, it's possible to typedef an array, using this construction :

typedef int table_t[N];

Here, table_t is now defined as an array of N int. Any variable declared such as table_t t; will now behave as a normal array of int.

The point of such construction is to be used as an argument type in a function, such as :

int doSomething(table_t t);

A relatively equivalent function prototype could have been :

int doSomething(int* t);

The merit of the first construction is that it enforces N as the size of the table. In many circumstances, it's safer to enforce this property, rather than relying on the programmer to properly figure out this condition.

Now it's all good, except that, in order to guarantee that the content of table will not be modified, it's necessary to use the const qualifier.

The following statement is relatively simple to understand :

int doSomething(const int* t);

Now, doSomething guarantee that it will not modify the content of the table passed as a pointer. Now, what about this almost equivalent construction ? :

int doSomething(const table_t t);

What is const here ? the content of the table, or the pointer to the table ? If it's the pointer which is const, is there another way (C90 compatible) to retain the ability to define the size of the table and to tell that its content will be const ?

Note that it's also necessary sometimes to modify the content of the table, so the const property cannot be embedded into the typedef definition.

[Edit] Thanks for the excellent answers received so far. To summarize :

  • The initial assumption of typedef enforcing size N was completely wrong. It basically behaves the same as a normal pointer.
  • The const property will also behave the same as if it was a pointer (in stark contrast with a typedef to a pointer type, as underlined by @random below)
  • To enforce a size (which was not the initial question, but end up being quite important now...), see Jonathan's answer
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

First, you are mistaken, the function prototypes

int doSomething(table_t t);
int doSomething(int* t);

are exactly equivalent. For function parameters, the first array dimension is always rewritten as a pointer. So there is no guarantee for the size of the array that is received.

const-qualification on arrays always applies to the base type of the array, so the two declarations

const table_t a;
int const a[N];

are equivalent, and for functions parameters we have

int doSomething(const table_t t);
int doSomething(int const* t);
over 4 years ago · Santiago Trujillo Report

0

The content of the table will be constant. Easily checked with this code.

#include<stdio.h>

typedef int table_t[3];
void doSomething(const table_t t)
{
    t++;    //No error, it's a non-const pointer.
    t[1]=3; //Error, it's a pointer to const.

}

int main()
{
    table_t t={1,2,3};
    printf("%d %d %d %ld",t[0],t[1],t[2],sizeof(t));
    t[1]=5;
    doSomething(t);
    return 0;
}
over 4 years ago · Santiago Trujillo Report

0

Array types and pointer types are not 100% equivalent, even in this context where you do ultimately get a pointer type for the function parameter. Your mistake is in assuming that const would have acted the same way if it were a pointer type.

To expand on ARBY's example:

typedef int table_t[3];
typedef int *pointer_t;

void doSomething(const table_t t)
{
    t++;    //No error, it's a non-const pointer.
    t[1]=3; //Error, it's a pointer to const.
}

void doSomethingElse(const pointer_t t)
{
    t++;    //Error, it's a const pointer.
    t[1]=3; //No error, it's pointer to plain int
}

It does act similarly to const int *, but const pointer_t is instead equivalent to int * const.

(Also, disclaimer, user-defined names ending with _t are not allowed by POSIX, they're reserved for future expansion)

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!