简体   繁体   中英

Accessing the memory location correctly to print the elements of an integer array using union in c

I am not how to use the unions to print the integer array here. Please correct me to get the out.

val.str is being printed correctly - as '+100-100-100+760' and the first element of the output array seems to be loaded the value correctly. Then the same memory is overridden and printing some garbage value and this is something I don't know how to solve.

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

//Variable Decalartion
int i = 0, x = 100, y = -100, z = -100, p = 760;
int len_str;
char temp[4];

union Data{
   char str[16];
   unsigned int buff[8];
};

void Init(union Data val, unsigned int *dest);

int main(void){
   union Data val;
   unsigned int x[8];

   Init(val,x);
   for (i = 0; i < 8; i++) 
   {
     printf("0x%04X ",x[i]);
   }

   return 0;
}

void Init(union Data val, unsigned int *dest)
{
   len_str = sizeof(val.str);

   sprintf(temp,"%+04d",x);
   strcpy(val.str,temp);

   sprintf(temp,"%+04d",y);
   strcat(val.str,temp);

   sprintf(temp,"%+04d",z);
   strcat(val.str,temp);

   sprintf(temp,"%+04d",p);
   strcat(val.str,temp);

   printf("%s\n",val.str);
   printf("\n");

   for (i = 0; i < len_str+1; i += 2) 
   {
      val.buff[i / 2] = ((unsigned int)(unsigned char)val.str[i] << 8) |
                            (unsigned int)(unsigned char)val.str[i + 1];
      dest[i / 2] = val.buff[i / 2];                      
   }
}

The output that is displayed is,

+100-100-100+760

0x2B31 0x401111C8 0x8049A58 0x8048782 0x0001 0xBFFB5E74 0xBFFB5E7C  0x401B0E4D

Thanks,

Akhil

There are many serious errors in this code.

As user694733 noted, char temp[4]; is too small. you have a buffer overflow there.

You make this call: Init(val,x); with unititialized data in val .

Note that the sizeof int is platform specific. You seem to assume it is 16bits ? It is 32bits on PCs and modern ARM processors. But may be 16bits on lower end processors.

At last I solved and please find the code,

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

//Variable Decalartion
int x = 1000, y = -1000, z = -1000, p = 760;
char temp[6];

union Data {
   char str[21];
   unsigned int buff[20];
};

void Init(union Data val);

int main(void){
   union Data val;
   Init(val);
   return 0;
}

void Init(union Data val)
{
   sprintf(temp,"%+05d",x);
   strcpy(val.str,temp);

   sprintf(temp,"%+05d",y);
   strcat(val.str,temp);

   sprintf(temp,"%+05d",z);
   strcat(val.str,temp);

   sprintf(temp,"%+05d",p);
   strcat(val.str,temp);

   printf("%s\n",val.str);
   printf("\n");

   printf("%s\n",val.buff);
   printf("\n");
}

The output,

+1000-1000-1000+0760

+1000-1000-1000+0760

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM