Skip to content

Conversation

@sprintersb
Copy link

Functions like unif01_CreateExternGen01 have currently a prototype of

unif01_Gen *unif01_CreateExternGen01 (char *name, double (*f_U01)(void))

i.e. they are alloed to change the string pointed to by name. This is not expected, and these functions actually DO NOT change the contents of name. Therefore, name should be const qualified:

unif01_Gen *unif01_CreateExternGen01 (const char *name, double (*f_U01)(void))

As a simple test case, consider

unsigned myrand (void)
{
static unsigned u;
return ++u;
}

const char myrand_name[] = "myrand";

unif01_Gen *gen_myrand (void)
{
return unif01_CreateExternGenBits (myrand_name, myrand);
}

testu01.c: In function 'gen_myrand':
testu01.c:13:40: warning: passing argument 1 of 'unif01_CreateExternGenBits' discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
13 | return unif01_CreateExternGenBits (myrand_name, myrand);
| ^~~~~~~~~~~
In file included from testu01.c:1:
$prefix/include/unif01.h:147:47: note: expected 'char *' but argument is of type 'const char *'
147 | unif01_Gen *unif01_CreateExternGenBits (char *name,
| ~~~~~~^~~~

* testu01/unif01.c (unif01_CreateExternGen01) (unif01_CreateExternGenBits, unif01_CreateExternGenBitsL): Pass name as const char* instead of as char*.
* testu01/unif01.tex: Same.

Functions like unif01_CreateExternGen01 have currently a prototype of

   unif01_Gen *unif01_CreateExternGen01 (char *name, double (*f_U01)(void))

i.e. they are alloed to change the string pointed to by name.
This is not expected, and these functions actually DO NOT
change the contents of name.  Therefore, name should be const qualified:

   unif01_Gen *unif01_CreateExternGen01 (const char *name, double (*f_U01)(void))

As a simple test case, consider

unsigned myrand (void)
{
   static unsigned u;
   return ++u;
}

const char myrand_name[] = "myrand";

unif01_Gen *gen_myrand (void)
{
   return unif01_CreateExternGenBits (myrand_name, myrand);
}

testu01.c: In function 'gen_myrand':
testu01.c:13:40: warning: passing argument 1 of 'unif01_CreateExternGenBits' discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
   13 |     return unif01_CreateExternGenBits (myrand_name, myrand);
      |                                        ^~~~~~~~~~~
In file included from testu01.c:1:
$prefix/include/unif01.h:147:47: note: expected 'char *' but argument is of type 'const char *'
  147 | unif01_Gen *unif01_CreateExternGenBits (char *name,
      |                                         ~~~~~~^~~~

	* testu01/unif01.c (unif01_CreateExternGen01)
	(unif01_CreateExternGenBits, unif01_CreateExternGenBitsL):
	Pass name as const char* instead of as char*.
	* testu01/unif01.tex: Same.
@sprintersb sprintersb changed the title Generator names should be passed as const char* and mot as char*. Generator names should be passed as const char* and not as char*. Dec 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant