D
dj
I've read section 6 of the FAQ, but still am a bit confused...
Please refer to the comments in the following
code for my questions:
-----------------------------------------------------------
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#define L_DATE 8
typedef struct
{
char date1[L_DATE];
char date2[L_DATE];
} my_record_t;
my_record_t my_rec;
void my_func(void);
int main(void)
{
char my_char[L_DATE];
memcpy(my_rec.date1, "20040101", L_DATE);
memcpy(my_rec.date2, "20040202", L_DATE);
/*
** I understand that "my_char" is the same as
** "&my_char[0]", but what about "&my_char"?
** I was surprised to find out that
** "my_char" == "&my_char"! (that is an exclamation,
** not a "not").
*/
printf("my_char = %p -> %p\n", &my_char, my_char );
/*
** I'm wondering if this is "defined" behavior, or if
** it just happens to work like this on my system?
**
** I'm asking because I saw some code which uses this
** "&my_char" in a memcpy, similar to what is done in
** my_func(), and I'm wondering if it is ok or not.
*/
printf("before: date1 = %.*s, date2 = %.*s\n",
L_DATE, my_rec.date1, L_DATE, my_rec.date2 );
my_func();
printf("after: date1 = %.*s, date2 = %.*s\n",
L_DATE, my_rec.date1, L_DATE, my_rec.date2 );
return EXIT_SUCCESS;
}
void my_func(void)
{
char local_date[L_DATE] = "19700101";
my_record_t *rec_p = &my_rec;
/*
** this doesn't look right to me, but it
** seems to work...
*/
memcpy( &rec_p->date2, &local_date, L_DATE );
}
--------------------------------------------------------
any help or comments are appreciated.
thanks,
-dj
Please refer to the comments in the following
code for my questions:
-----------------------------------------------------------
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#define L_DATE 8
typedef struct
{
char date1[L_DATE];
char date2[L_DATE];
} my_record_t;
my_record_t my_rec;
void my_func(void);
int main(void)
{
char my_char[L_DATE];
memcpy(my_rec.date1, "20040101", L_DATE);
memcpy(my_rec.date2, "20040202", L_DATE);
/*
** I understand that "my_char" is the same as
** "&my_char[0]", but what about "&my_char"?
** I was surprised to find out that
** "my_char" == "&my_char"! (that is an exclamation,
** not a "not").
*/
printf("my_char = %p -> %p\n", &my_char, my_char );
/*
** I'm wondering if this is "defined" behavior, or if
** it just happens to work like this on my system?
**
** I'm asking because I saw some code which uses this
** "&my_char" in a memcpy, similar to what is done in
** my_func(), and I'm wondering if it is ok or not.
*/
printf("before: date1 = %.*s, date2 = %.*s\n",
L_DATE, my_rec.date1, L_DATE, my_rec.date2 );
my_func();
printf("after: date1 = %.*s, date2 = %.*s\n",
L_DATE, my_rec.date1, L_DATE, my_rec.date2 );
return EXIT_SUCCESS;
}
void my_func(void)
{
char local_date[L_DATE] = "19700101";
my_record_t *rec_p = &my_rec;
/*
** this doesn't look right to me, but it
** seems to work...
*/
memcpy( &rec_p->date2, &local_date, L_DATE );
}
--------------------------------------------------------
any help or comments are appreciated.
thanks,
-dj