to my understanding, defining a function parameter as "const" means that
the function is not going to change it.
Why does the compiler says "return discards qualifiers from pointer
target type" when I *access* a member of an argument defined as const?
Please see the code below:
/*** START TEST.C ***/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_NAME_SIZE 80
#define MY_NAME "This is my name"
struct my_s
{
char mys_name[MAX_NAME_SIZE+1];
};
void set_mys_name(st ruct my_s *, const char *);
char *get_mys_name(c onst struct my_s *);
int main(void) {
struct my_s *mysp;
mysp = malloc(sizeof *mysp);
set_mys_name(my sp, MY_NAME);
printf("My name is %s\n", get_mys_name(my sp));
return (0);
}
void
set_mys_name(st ruct my_s *mys, const char *name)
{
if(!mys) return;
strncpy(mys->mys_name, name, MAX_NAME_SIZE);
}
char *
get_mys_name(co nst struct my_s *mys)
{
if(!mys) return (NULL);
return (mys->mys_name);
}
/*** START TEST.C ***/
test.c: In function `get_mys_name':gcc -Wall -o test test.c
test.c:39: warning: return discards qualifiers from pointer target type
My name is This is my name./test
Thank you!
--
Pietro Cerutti
PGP Public Key:
http://gahr.ch/pgp