C/C++ :: Sizeof (struct) Returns 6 More Bytes Than Actual Struct Size?
Sep 14, 2014
#include <stdio.h>
#define MAX_USERS 20
struct {
char ID[10];
char Name[40];
int Pos;
[Code] .....
I was attempting something weired with address to move data around when I discovered that the size of the array is not what I expected. I am passing this structure as &Users to a function that declares it as a void *, then I can deal with chunks of data (memmove) and not have to worry about index or things like that. However...sizeof is returning something I do not understand.
View 9 Replies
ADVERTISEMENT
Jul 11, 2013
I was wondering why, in C, the sizeof of a struct is larger than the the sum of all the sizeofs of it's members. It only seems to be by a few bytes, but as a bit of a perfectionist I fine this a bit annoying.
View 1 Replies
View Related
Dec 14, 2014
I'm having trouble figuring out how to find the size of an array program that involves "struct."
#include <iostream>
using namespace std;
struct d{
char* a;
float b;
int c;
[code].....
When I run this program, the output is 80(for my compiler). That would mean that each element in the array is 16 bytes but I don't understand how struct d is 16 bytes.
View 1 Replies
View Related
Apr 20, 2013
typedef struct Element Element;
struct Element {
char x;
double* y;
[Code] .....
This one with y pointer gives 8
typedef struct Element Element;
struct Element {
char x;
double y;
[Code] ....
This one with a normal y variable gives 12
View 7 Replies
View Related
Mar 6, 2015
The WinAPI has a struct like this for raw input:
Code:
typedef struct tagRAWINPUT { RAWINPUTHEADER header;
union {
RAWMOUSE mouse;
RAWKEYBOARD keyboard;
RAWHID hid;
} data;
[code]...
The definition of the struct doesn't show it but the documentation says that bRawData is variable length. sizeof(RAWINPUT) will not be the correct size when the data field is of RAWHID type so how do you allocate a variable with automatic storage type that has the right size for the entire struct? You can get a header that has the size for the entire struct but how do you actually allocate storage space for the data without using malloc? I've seen some example code that used a char array but that violates aliasing rules and there are also alignment issues with that approach.
View 5 Replies
View Related
Feb 9, 2015
I'd wrote a program to encrypt a message within a bmp file using my own structs and all for everything (yes, call me a ........head) The program works but for some weird ........ing reason I was forced to subtract 2 bytes from the header size to get the correct value. I've narrowed down the issue to my BmpFileHeader struct.
Here's a short program that demonstrates the issue:
Code:
#include <stdio.h>
#include <stdlib.h>
#define BYTE unsigned char
#define WORD unsigned short
#define DWORD unsigned long
#define LONG signed int
[Code] .....
Tried with both gcc and TinyCC and got the same result so it doesent seem to be a compiler bug. Microsoft's structures though are giving the correct size, even though they have the exact same definition.
Microsoft's defines:
Code:
// windef.h
typedef unsigned long DWORD;
typedef unsigned char BYTE;
typedef unsigned short WORD;
[Code] .....
View 5 Replies
View Related
Mar 15, 2015
how to accommodate double-size:8 bytes in 4 bytes pointer in 32bit system.
View 1 Replies
View Related
Feb 28, 2015
Im having trouble creating a struct within a struct node. the program suppose to hold students firstname, lastname, and gpa in a node therefore creating my linked list. Line 26 keeps saying that cannot convert parameter 2 from 'studentType to std::string
#include <iostream>
#include <string>
using namespace std;
struct studentType{
string firstname;
string lastname;
double gpa;
[code].....
View 2 Replies
View Related
Mar 1, 2013
I do not understand how I can implement this.If fread != to at least 8 bytes then do THIS: printf (" your file is near the end of file", fread result);
View 8 Replies
View Related
Sep 26, 2014
How can I see the actual byte size of the pointer as output in the command prompt?
#include <iostream>
using namespace std;
int main () {
[Code].....
View 2 Replies
View Related
Jun 5, 2012
"
#include <stdio.h>
struct datastructure {
char character;
};
void function(struct datastructure** ptr);
[Code] ....
These codes give these errors:
error: request for member 'character' in '* ptr', which is of non-class type 'datastructure*'
error: request for member 'character' in '* ptr', which is of non-class type 'datastructure*'
These errors are related to
"
*ptr->character='a';
printf("Ptr: %c",*ptr->character);
"
I want to access "character" data inside the structure "trial" by a pointer to pointer "ptr" inside function "function",but I couldn't find a way to do this.
View 3 Replies
View Related
Mar 9, 2012
I just read and have known for a while that classes are private (members and inheritance) by default and structs are public. But my question then comes what if.. a struct inheriting from a class or a class inheriting from a struct?
View 3 Replies
View Related
Dec 24, 2013
to return the array i shall make a pointer function thats ok.. but how do I get the size return if i dont know the size?
if I need to make AXB=C and output C my new array doesnt have a size..
View 7 Replies
View Related
Apr 10, 2013
how to split a file in equal size and when clicking on split button it split the files as well as encrypt split parts and the size information are automatically stored in groupbox and save all splitted files in folder.
View 1 Replies
View Related
Oct 24, 2014
So why does this not work? As an example:
struct example {
int x; int y;
};
int main() {
example variable;
variable.x = 1;
variable.y = 2;
map<example, int> something;
something[variable] = 3;
return 0;
}
And I get a very long error message from the compiler. I tried using an enum as the key as well and that worked just fine, but I guess that's just the same as having an integer as the key. But anyway, what's the problem with this? Is there any way to make it work?
View 4 Replies
View Related
Mar 6, 2015
how can I call and print the Pointer:
ptr->address
Code: #include<stdio.h>
struct account {
int address;
int value;
[code]....
View 5 Replies
View Related
Jul 9, 2014
So i create a simple structure,that has a firstname, lastname, age, dateofbirth.I would like it if i type what I am searching for print out there is one or not.But i have some trouble with equals(in array).. i type in the same "keresettnev" as "tanulok[i].Firstname" but it thinks not the same. (it works with age..)
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student {
char *Firstname[40];
char *Lastname[40];
int age;
[Code] ....
View 12 Replies
View Related
Jun 19, 2014
I am using Beej's Guide to Network Programming Using Internet Sockets. I'm just curious why I need to zero out the rest of the struct?
Code:
int main(){
int sockfd;
struct sockaddr_in my_addr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
[Code].....
View 8 Replies
View Related
Apr 22, 2014
Code:
#include <stdio.h>
struct database {
int id_number;
int age;
float salary;
[Code] ....
When I compile, I get an error:
test.c|18|error: incompatible type for argument 1 of 'printf'|
note: expected 'const char *' but argument is of type 'float'|
I thought employee.salary is a float but the compiler expected 'const char'. How do I make this work?
View 4 Replies
View Related
May 7, 2013
I am working on a program which uses external hardware to acquire data. we have the option to use hardware from two different companies, each with it's own driver. However both do the same job. My program is meant to read data packets which are structured as:
typedef struct _data {
DWORD ID;
BYTE bf;
BYTE bd;
BYTE bData[8];
DWORD bt;
} data;
Now both the companis use the same data structure, but the first has defined the structure, let's say, as:
typedef struct _dataX {
DWORD ID_X;
BYTE bf_X;
BYTE bd_X_;
BYTE bData_X[8];
DWORD bt_X;
} dataX;
[code].....
I am programming my software so as t allow the customer to use the hardware of their choice. Simply select the card they are using and our program should be able to take care of the rest of stuff. I am using #ifdef directives to include the header for the corresponding hardware dll. Now I want to define my own data struct of the type:
typedef struct _dataMY
{
DWORD ID_MY;
BYTE bf_MY;
BYTE bd_MY_;
BYTE bData_MY[8];
DWORD bt_MY;
}
[code]....
and when I use _dataMY.ID_MY .. it should directly be able to see if it is _dataX.ID_X or _dataY.ID_Y, based upon the directive I have used earlier.
View 3 Replies
View Related
Jun 21, 2014
I've been working on a path-tracer for some time, and all along I've used structs instead of classes for vectors and matrices. Today I changed all of them to classes instead, changing none of the actual function bodies themselves, and the results were miserable to say the least.
Here's a render form before the change: [URL] ....
And here's the same render after: [URL] ....
Why this is happening, considering that none of the actual function-bodies have been changed, except for what little is needed to make the change from class to struct.
View 5 Replies
View Related
Oct 29, 2013
I have the following code:
struct mystruct {
char fieldA[5];
char fieldB[7];
};
void dosomething(struct mystruct* pms) {
[Code] ....
Is there any problem doing that, even for a C89/90 compiler?
View 3 Replies
View Related
Apr 4, 2013
Suppose I have a struct with 20 members. I want to assign each of those structs with values. Instead of accesing by explicit convention, i.e.:
Code: struct.member1 = 1;
struct.member2 = 2;
...
...
...
struct.member20 = 6;
Is there any way to encapsulate it in a for loop and use an iterator variable to indicate the member? i.e.:
Code: for (int i = 0; i < 20; i++)
struct.i = i;
The above is pseudocode but Im sure you get what Im trying to do?
View 2 Replies
View Related
May 2, 2013
So this is the last part of a program I've been working on for four weeks now. This question may be a tough one considering the amount of files included in the program.
The program is to read in a file of requests between two cities, read in a file of flights and cities that occur between the cities. It then checks to see if there is a path between the flights and output an itinerary. I have the correct itinerary outputting, but when attempting to output the associated flight number and price according to the city, I am getting odd data. How can I output the correct flight number and price associated with each flight on the itinerary.
I'll post out the output I am currently getting and the section where I am outputting the data. I'm sure I'll need to post more files so the program can be understood.
Don't want the code done for me, just a point in the right direction! I don't want to let this program defeat me!
Output: Code: Request is to fly from Atlanta to San-Diego.The flight itinerary is:
Flight # From To Cost
10 Atlanta Chicago $134529069
10 Chicago Miami $134529069
10 Miami Dallas $134529069
10 Dallas San-Francisco $134529069
10 San-Francisco San-Diego $134529069
This function finds a path between cities. Code: bool flightMap::IsPath(string originCity, string destinationCity){
StackClass aStack, bStack;
flightStruct flightRec;
string topCity, nextCity;
bool success;
int index = 0;
[Code].....
View 1 Replies
View Related
May 10, 2013
I am working on an assignment identical to another post from a couple years ago, for reference here is the thread:
array of pointers to structures sorting addresses by zip code
They way it is written on that thread is almost identical to the way the teacher implied to have it done (only wrote part of the input block). But I am having an error:
When it gets to the output section it outputs then next name along with the zip code... I tried strncpy and strxfrm but both cause more problems than they did work.
The last part of the project is to have the output put out in order of least zip code to most zip code (00000<99999), so this is causing me a real problem and I do not see what exactly is making this happen.
Here is my code (we dont HAVE to use gets but professor suggested using it for this assignment, next lab is to rewrite this using files and fgets rather than I/O redirection):
header.h Code: #ifndef lab_6b_7b_Header_h
#define lab_6b_7b_Header_h
//header file intiating other headers
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
[code]....
I have not started the sorting code because I cannot get past this, but once I have proper zip codes I am sure I can make a sort function no problem.
I am using xcode with some breaks to read variables as various points and do not notice anything wrong until it makes it to the output functions, although this page briefly pops up between input and output functions when the breaks are up:
View 8 Replies
View Related
May 14, 2013
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct BOOK
{
int* price;
} Book;
[Code] ....
View 1 Replies
View Related