J
Jean-Marc
Hello all ,
i have a strange behaviour with this code.
File: func.h
--------------
#ifndef __FUNC_H__
#define __FUNC_H__
void init_value(void);
char *get_value(void);
#endif
file: func.c
--------------
#include<stdio.h>
#include<string.h>
#define MY_SIZE 50
static char szValue[MY_SIZE] = "";
void init_value(void)
{
strcpy(szValue, "TEST TEST TEST");
}
char *get_value(void)
{
return szValue;
}
file: main.c
--------------
#include<stdlib.h>
#include<stdio.h>
#include "func.h"
int main(void)
{
char *p = NULL;
init_value();
p = get_value();
if(p == NULL)
{
printf("p is NULL here.\n");
}
else
{
printf("p : %s\n", p);
/* here, under z/OS => *p= '\0' */
}
return EXIT_SUCCESS;
}
This code compiles without errors/warings using several compilers, under
several OS:
Windows, Unix (SUN & AIX) & z/OS
The problem is that under z/OS, get_value() does not return "TEST TEST TEST"
but "".
REMARK: if i change the following
static char szValue[MY_SIZE] by char szValue[MY_SIZE], then it works.
I wonder if this code is correct, meaning, does it invoke an undefined
behavior?
And if yes, why ? I have carefully read the C norma, but i can not find any
related topics.
Thanks for the help,
Jean-marc
--
i have a strange behaviour with this code.
File: func.h
--------------
#ifndef __FUNC_H__
#define __FUNC_H__
void init_value(void);
char *get_value(void);
#endif
file: func.c
--------------
#include<stdio.h>
#include<string.h>
#define MY_SIZE 50
static char szValue[MY_SIZE] = "";
void init_value(void)
{
strcpy(szValue, "TEST TEST TEST");
}
char *get_value(void)
{
return szValue;
}
file: main.c
--------------
#include<stdlib.h>
#include<stdio.h>
#include "func.h"
int main(void)
{
char *p = NULL;
init_value();
p = get_value();
if(p == NULL)
{
printf("p is NULL here.\n");
}
else
{
printf("p : %s\n", p);
/* here, under z/OS => *p= '\0' */
}
return EXIT_SUCCESS;
}
This code compiles without errors/warings using several compilers, under
several OS:
Windows, Unix (SUN & AIX) & z/OS
The problem is that under z/OS, get_value() does not return "TEST TEST TEST"
but "".
REMARK: if i change the following
static char szValue[MY_SIZE] by char szValue[MY_SIZE], then it works.
I wonder if this code is correct, meaning, does it invoke an undefined
behavior?
And if yes, why ? I have carefully read the C norma, but i can not find any
related topics.
Thanks for the help,
Jean-marc
--