Skip to content

Instantly share code, notes, and snippets.

@sawaken
Last active August 29, 2015 14:07
Show Gist options
  • Save sawaken/b0fd010829adc0eebb6d to your computer and use it in GitHub Desktop.
Save sawaken/b0fd010829adc0eebb6d to your computer and use it in GitHub Desktop.
Declaration of pointer variable with qualifier "const" in C.
/* gcc 4.7.2 */
#include <stdlib.h>
void f(int** const a)
{
a = malloc(5 * sizeof(int)); // compile error
a[0] = malloc(sizeof(int));
*a[0] = 100;
}
void g(int* const * a)
{
a = malloc(5 * sizeof(int));
a[0] = malloc(sizeof(int)); // compile error
*a[0] = 100;
}
void h(int const ** a)
{
a = malloc(5 * sizeof(int));
a[0] = malloc(sizeof(int));
*a[0] = 100; // compile error
}
void h2(const int ** a)
{
a = malloc(5 * sizeof(int));
a[0] = malloc(sizeof(int));
*a[0] = 100; // compile error
}
/*
multi qualify
*/
void f_h(int const ** const a)
{
a = malloc(5 * sizeof(int)); // compile error
a[0] = malloc(sizeof(int));
*a[0] = 100; // compile error
}
/*
using []
*/
void x(int * const a[])
{
a = malloc(5 * sizeof(int));
a[0] = malloc(sizeof(int)); // compile error
*a[0] = 100;
}
void y(int const * a[])
{
a = malloc(5 * sizeof(int));
a[0] = malloc(sizeof(int));
*a[0] = 100; // compile error
}
void y2(const int * a[])
{
a = malloc(5 * sizeof(int));
a[0] = malloc(sizeof(int));
*a[0] = 100; // compile error
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment