473,385 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how are the variables stored in memory

2
HI all,
can anyone tell me output of this program

#include<stdio.h>
#include<conio.h>
struct {
char a1;
char a2;
char a3;
int a4;
}st;
void main()
{
char buff[]="12345";
memcpy(st,buff,5);
printf("value of a4=%d",st.a4);
}
Oct 26 '07 #1
4 1934
amitpatel66
2,367 Expert 2GB
HI all,
can anyone tell me output of this program

#include<stdio.h>
#include<conio.h>
struct {
char a1;
char a2;
char a3;
int a4;
}st;
void main()
{
char buff[]="12345";
memcpy(st,buff,5);
printf("value of a4=%d",st.a4);
}
You have a program with you. why dont You execute the program by yourself and find the output?
If it errors out, POST back your error message so that we can help you!!
Oct 26 '07 #2
yash04
2
output is 14436.
can u plz tell me hows last two byte are stored in memory ?
Oct 26 '07 #3
amitpatel66
2,367 Expert 2GB
output is 14436.
can u plz tell me hows last two byte are stored in memory ?
What does last two bytes refer to?
Are to refering to 45 from the string 12345??
Oct 26 '07 #4
Banfa
9,065 Expert Mod 8TB
HI all,
can anyone tell me output of this program

#include<stdio.h>
#include<conio.h>
struct {
char a1;
char a2;
char a3;
int a4;
}st;
void main()
{
char buff[]="12345";
memcpy(st,buff,5);
printf("value of a4=%d",st.a4);
}
No because it is dependent on the structure packing rules used in you compiler/platform and its endianess.

However you where to print sizeof st I suspect you would find it was 8 (or possibly 6). Since you are only copying 5 bytes you are not filling the entire memory region for st and particularly you are not filling all bytes of a4 (which probably has 4 bytes not 2 has you suggest) so it's output value is completely random in at least 1 and probably 3 bytes.


Perhaps you should explain what it is you are trying to do?
Oct 26 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Prasad | last post by:
Where are the const variables actually stored in memory. I mean If they are stored in data segment(external & static variables)or stack segment(local) how the compiler knows that it is read only...
122
by: Einar | last post by:
Hi, I wonder if there is a nice bit twiddling hack to compare a large number of variables? If you first store them in an array, you can do: for (i = 0; i < n; i++) { if (array != value) {...
31
by: Harry Simpson | last post by:
I've come from the old ASP camp where session variables were not used. When i started using ASP.NET in 2001, I started using them again because it was ok from what I'd read. I've been merrily...
6
by: Roman | last post by:
Which is better when passing values between forms?
27
by: Madhav | last post by:
Hi all, I did not understand why do the global vars are initialized to NULL where as the block level variables have random values? I know that the C standard requires this as was mentioned in a...
18
by: akhil.misra | last post by:
where does a static variable inside a function get stored?? where does it go in memory?? like Global goes in RAM, Local variable goes on Stack... but how does static variable takes place in memory??
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
7
by: kr | last post by:
Hi All, Suppose I consider a sample program as given below:- #include<stdio.h> #include<stdlib.h> int i; int main() { char *test(int i); char *tmp = NULL;
5
by: vnpatriot7 | last post by:
Hi everybody, I have two questions: 1) About memory space in C++ 2) About global and static variable As what I read somewhere that after compilation process which translate C++ code into...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.