P
Pietro Cerutti
i Group,
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(struct my_s *, const char *);
char *get_mys_name(const struct my_s *);
int main(void) {
struct my_s *mysp;
mysp = malloc(sizeof *mysp);
set_mys_name(mysp, MY_NAME);
printf("My name is %s\n", get_mys_name(mysp));
return (0);
}
void
set_mys_name(struct my_s *mys, const char *name)
{
if(!mys) return;
strncpy(mys->mys_name, name, MAX_NAME_SIZE);
}
char *
get_mys_name(const struct my_s *mys)
{
if(!mys) return (NULL);
return (mys->mys_name);
}
/*** START TEST.C ***/
test.c: In function `get_mys_name':
test.c:39: warning: return discards qualifiers from pointer target type
My name is This is my name
Thank you!
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(struct my_s *, const char *);
char *get_mys_name(const struct my_s *);
int main(void) {
struct my_s *mysp;
mysp = malloc(sizeof *mysp);
set_mys_name(mysp, MY_NAME);
printf("My name is %s\n", get_mys_name(mysp));
return (0);
}
void
set_mys_name(struct my_s *mys, const char *name)
{
if(!mys) return;
strncpy(mys->mys_name, name, MAX_NAME_SIZE);
}
char *
get_mys_name(const struct my_s *mys)
{
if(!mys) return (NULL);
return (mys->mys_name);
}
/*** START TEST.C ***/
test.c: In function `get_mys_name':
test.c:39: warning: return discards qualifiers from pointer target type
My name is This is my name
Thank you!