I am having problems accessing certain structure members, namely char* members. Instead of reading along the Char* array, it is incrementing it. What am I doing wrong?
#include <stdio.h>
#include <stdlib.h>
#define NAME 1162690894 // read as 4 bytes, spells NAME
void prnt_block( struct data_block*);
void prntNAME(char**);
struct data_block{
char *cname;
};
int main(void)
{
struct data_block data[10];
data[0].cname = (char *)calloc( 4, sizeof( char ) );
data[0].cname = (char *)NAME;
printf("1st value of cname: %d\n", data->cname);
// call func
prnt_block(&data[0]);
return 0;
} // end o' main
void prnt_block(struct data_block *db)
{
prntNAME(&(db->cname));
}
void prntNAME(char **n)
{
int i;
printf("2nd value of cname: %d\n", *n);
for(i = 0; i < 4; i++)
{
printf("%c", *n);
(*n) = (*n) + 1;
}
printf("\n"
printf("3rd value of cname: %d\n", *n);
}
if you run this it will increment the value of cname, instead of reading along the char* array, printing the byte values as chars. The result is NOPQ instead of NAME, as I want.
#include <stdio.h>
#include <stdlib.h>
#define NAME 1162690894 // read as 4 bytes, spells NAME
void prnt_block( struct data_block*);
void prntNAME(char**);
struct data_block{
char *cname;
};
int main(void)
{
struct data_block data[10];
data[0].cname = (char *)calloc( 4, sizeof( char ) );
data[0].cname = (char *)NAME;
printf("1st value of cname: %d\n", data->cname);
// call func
prnt_block(&data[0]);
return 0;
} // end o' main
void prnt_block(struct data_block *db)
{
prntNAME(&(db->cname));
}
void prntNAME(char **n)
{
int i;
printf("2nd value of cname: %d\n", *n);
for(i = 0; i < 4; i++)
{
printf("%c", *n);
(*n) = (*n) + 1;
}
printf("\n"
printf("3rd value of cname: %d\n", *n);
}
if you run this it will increment the value of cname, instead of reading along the char* array, printing the byte values as chars. The result is NOPQ instead of NAME, as I want.