Search Results

Search found 2566 results on 103 pages for 'struct'.

Page 2/103 | < Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >

  • Operator precedence and struct definition in C

    - by Yktula
    struct struct0 { int a; }; struct struct1 { struct struct0 structure0; int b; } rho; &rho->structure0; /* Reference 1 */ (struct struct0 *)rho; /* Reference 2 */ (struct struct0)rho; /* Reference 3 */ From reference 1, does the compiler take the address of rho, and then access structure0, or vice-versa? What does the line at reference 2 do? Since structure0 is the first member of struct1, would reference 3 be equivalent to reference 1?

    Read the article

  • undefined C struct forward declaration

    - by robUK
    Hello, I have a header file port.h, port.c, and my main.c I get the following error: 'ports' uses undefined struct 'port_t' I thought as I have declared the struct in my .h file and having the actual structure in the .c file was ok. I need to have the forward declaration as I want to hide some data in my port.c file. In my port.h I have the following: /* port.h */ struct port_t; port.c: /* port.c */ #include "port.h" struct port_t { unsigned int port_id; char name; }; main.c: /* main.c */ #include <stdio.h> #include "port.h" int main(void) { struct port_t ports; return 0; } Many thanks for any suggestions,

    Read the article

  • Declaring a prototype of type "struct" - C

    - by Jamie Keeling
    I've been racking my brains on this for a while, I'm simply trying to create a method that returns a struct as I wish to return two int's. My prototype for the method is as follows: typedef struct RollDice(); Also the method itself: typedef struct RollDice() { diceData diceRoll; diceRoll.dice1 = 0; diceRoll.dice2 = 0; return diceRoll; } The compiler shows the error: "Syntax error: ')'" for both the prototype and actual method. The struct itself: typedef struct { int dice1; int dice2; }diceData; Is it obvious where I'm going wrong? I've tried everything I can think of. Thanks

    Read the article

  • C Struct as an argument

    - by Brian
    I'm wondering what's the difference between sample1 and sample2. Why sometimes I have to pass the struct as an argument and sometimes I can do it without passing it in the function? and how would it be if samplex function needs several structs to work with? would you pass several structs as an argument? struct x { int a; int b; char *c; }; void sample1(struct x **z;){ printf(" first member is %d \n", z[0]->a); } void sample2(){ struct x **z; printf(" first member is %d \n", z[0]->a); // seg fault } int main(void) { struct x **z; sample1(z); sample2(); return 0; }

    Read the article

  • Typedef and Struct in C and H files

    - by Leif Andersen
    I've been using the following code to create various struct, but only give people outside of the C file a pointer to it. (Yes, I know that they could potentially mess around with it, so it's not entirely like the private keyword in Java, but that's okay with me). Anyway, I've been using the following code, and I looked at it today, and I'm really surprised that it's actually working, can anyone explain why this is? In my C file, I create my struct, but don't give it a tag in the typedef namespace: struct LABall { int x; int y; int radius; Vector velocity; }; And in the H file, I put this: typedef struct LABall* LABall; I am obviously using #import "LABall.h" in the c file, but I am NOT using #import "LABall.c" in the header file, as that would defeat the whole purpose of a separate header file. So, why am I able to create a pointer to the LABall* struct in the H file when I haven't actually included it? Does it have something to do with the struct namespace working accross files, even when one file is in no way linked to another? Thank you.

    Read the article

  • Bad_alloc exception when using new for a struct c++

    - by bsg
    Hi, I am writing a query processor which allocates large amounts of memory and tries to find matching documents. Whenever I find a match, I create a structure to hold two variables describing the document and add it to a priority queue. Since there is no way of knowing how many times I will do this, I tried creating my structs dynamically using new. When I pop a struct off the priority queue, the queue (STL priority queue implementation) is supposed to call the object's destructor. My struct code has no destructor, so I assume a default destructor is called in that case. However, the very first time that I try to create a DOC struct, I get the following error: Unhandled exception at 0x7c812afb in QueryProcessor.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0012f5dc.. I don't understand what's happening - have I used up so much memory that the heap is full? It doesn't seem likely. And it's not as if I've even used that pointer before. So: first of all, what am I doing that's causing the error, and secondly, will the following code work more than once? Do I need to have a separate pointer for each struct created, or can I re-use the same temporary pointer and assume that the queue will keep a pointer to each struct? Here is my code: struct DOC{ int docid; double rank; public: DOC() { docid = 0; rank = 0.0; } DOC(int num, double ranking) { docid = num; rank = ranking; } bool operator>( const DOC & d ) const { return rank > d.rank; } bool operator<( const DOC & d ) const { return rank < d.rank; } }; //a lot of processing goes on here; when a matching document is found, I do this: rank = calculateRanking(table, num); //if the heap is not full, create a DOC struct with the docid and rank and add it to the heap if(q.size() < 20) { doc = new DOC(num, rank); q.push(*doc); doc = NULL; } //if the heap is full, but the new rank is greater than the //smallest element in the min heap, remove the current smallest element //and add the new one to the heap else if(rank > q.top().rank) { q.pop(); cout << "pushing doc on to queue" << endl; doc = new DOC(num, rank); q.push(*doc); } Thank you very much, bsg.

    Read the article

  • c# pinvoke marshall struct

    - by Wouter Roux
    Hi, I have an unmanaged struct I'd like to marshal to c# that looks basically like this: struct DateTimeStruct{ double datetimestamp; }; struct MyStruct{ char firstname[40]; char lastname[40]; DateTimeStruct bday; unsigned integer bool1; int val1; }; What is the the correct c# declaration?

    Read the article

  • struct.error: unpack requires a string argument of length 4

    - by Thomas O
    Python says I need 4 bytes for a format code of "BH": struct.error: unpack requires a string argument of length 4 Here is the code, I am putting in 3 bytes as I think is needed: major, minor = struct.unpack("BH", self.fp.read(3)) "B" = Unsigned char (1 byte) + "H" unsigned short (2 bytes) = 3 bytes (!?) struct.calcsize("BH") says 4 bytes.

    Read the article

  • C++ struct containing unsigned char and int bug

    - by powerfear
    Ok i have a struct in my C++ program that is like this: struct thestruct { unsigned char var1; unsigned char var2; unsigned char var3[2]; unsigned char var4; unsigned char var5[8]; int var6; unsigned char var7[4]; }; When i use this struct, 3 random bytes get added before the "var6", if i delete "var5" it's still before "var6" so i know it's always before the "var6". But if i remove the "var6" then the 3 extra bytes are gone. If i only use a struct with a int in it, there is no extra bytes. So there seem to be a conflict between the unsigned char and the int, how can i fix that?

    Read the article

  • Modify existing struct alignment in Visual C++

    - by Crend King
    Is there a way to modify the member alignment of an existing struct in Visual C++? Here is the background: I use an 3rd-party library, which uses several structs. To fill up the structs, I pass the address of the struct instance to some functions. Unfortunately, the functions only returns unaligned buffer, so that data of some members are always wrong. /Zp is out of choice, since it breaks the other parts of the program. I know #pragma pack modifies the alignment of the following struct, but I do not want to copy the structs into my code, for the definitions in the library might change in the future. Sample code: test.h: struct am_aligned { BYTE data1[10]; ULONG data2; }; test.cpp: include "test.h" // typedef alignment(1) struct am_aligned am_unaligned int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { char buffer[20] = {}; for (int i = 0; i < sizeof(unaligned_struct); i++) { buffer[i] = i; } am_aligned instance = *(am_aligned*) buffer; return 0; } instance.data2 is 0x0f0e0d0c, while 0x0d0c0b0a is desired. The commented line does not work of course. Thanks for help!

    Read the article

  • struct size is different from typedef version?

    - by samoz
    I have the following struct declaration and typedef in my code: struct blockHeaderStruct { bool allocated; unsigned int length; }; typedef struct blockHeaderStruct blockHeader; When I do sizeof(blockheader), I get the value of 4 bytes back, but when I do sizeof(blockHeaderStruct), I get 8 bytes. Why is this happening? Why am I not getting 5 back instead?

    Read the article

  • C++: why a self pointer of a struct automatically changes to void*

    - by Stone
    struct ptr{ int node; ptr *next; ptr(){} ptr(int _node, ptr *_next){ node=_node; next=_next; } }; struct list_t{ ptr *sht; int size; void push(int node){ size++; sht=new ptr(node,sht); } }shthead[100001], comp[200001], tree[200001]; The struct ptr is a smart pointer, be used as a linked list. But when I debug the code in gdb, I found that the ptr*'s were all converted to void*. GDB output: (gdb) pt ptr type = struct ptr { int node; void *next; public: ptr(void); ptr(int, void *); } However, I can still see the data of the struct if I covert them back to ptr* in gdb. What's the reason for this please?

    Read the article

  • Regarding C typedef struct

    - by Bruce Duncan
    I have multiple instances of typedef struct box so box box1, box box2 etc. The members of the struct are length, width, height etc. typedef struct { int width; int height; } box; box box1; box box2; How can I create a function that operates on all the width members of each box instance? My confusion is how do I pass a pointer to a typedef struct member that works across all instances of box. I know how to pass a pointer to a specific instance member like box1.width but how to pass .width and then do box1.width=value; box2.width=value; box3.width=value; within the function?

    Read the article

  • C Pointer Question: &(*struct->struct)

    - by NTek
    I have a struct defined with the structure as follows (names are different) struct str1 { int field1; struct str2; } And I have a *str1 in a function. I'd like to get a pointer to str2. So I tried &(str1->str2) and was hoping this would return a pointer to str2. Is this incorrect? It doesn't seem to be working. How would I get a pointer to str2 if given a pointer to str1?

    Read the article

  • What is NSString in struct?

    - by 4thSpace
    I've defined a struct and want to assign one of its values to a NSMutableDictionary. When I try, I get a EXC_BAD_ACCESS. Here is the code: //in .h file typedef struct { NSString *valueOne; NSString *valueTwo; } myStruct; myStruct aStruct; //in .m file - (void)viewDidLoad { [super viewDidLoad]; aStruct.valueOne = @"firstValue"; } //at some later time [myDictionary setValue:aStruct.valueOne forKey:@"key1"]; //dies here with EXC_BAD_ACCESS This is the output in debugger console: (gdb) p aStruct.valueOne $1 = (NSString *) 0xf41850 Is there a way to tell what the value of aStruct.valueOne is? Since it is an NSString, why does the dictionary have such a problem with it? ------------- EDIT ------------- This edit is based on some comments below. The problem appears to be in the struct memory allocation. I have no issues assigning the struct value to the dictionary in viewDidLoad, as mentioned in one of the comments. The problem is that later on, I run into an issue with the struct. Just before the error, I do: po aStruct.oneValue Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: KERN_PROTECTION_FAILURE at address: 0x00000000 0x9895cedb in objc_msgSend () The program being debugged was signaled while in a function called from GDB. GDB has restored the context to what it was before the call. To change this behavior use "set unwindonsignal off" Evaluation of the expression containing the function (_NSPrintForDebugger) will be abandoned. This occurs just before the EXC_BAD_ACCESS: NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"MM-dd-yy_HH-mm-ss-A"]; NSString *date = [formatter stringFromDate:[NSDate date]]; [formatter release]; aStruct.valueOne = date; So the memory issue is most likely in my releasing of formatter. The date var has no retain. Should I instead be doing NSString *date = [[formatter stringFromDate:[NSDate date]] retain]; Which does work but then I'm left with a memory leak.

    Read the article

  • segfault on vector<struct>

    - by Andre
    Hello, I created a struct to hold some data and then declared a vector to hold that struct. But when I do a push_back I get damn segfault and I have no idea why! My struct is defines as: typedef struct Group { int codigo; string name; int deleted; int printers; int subpage; /*included this when it started segfaulting*/ Group(){ name.reserve(MAX_PRODUCT_LONG_NAME); } ~Group(){ name.clear(); } Group(const Group &b) { codigo = b.codigo; name = b.name; deleted = b.deleted; printers = b.printers; subpage = b.subpage; } /*end of new stuff*/ }; Originally, the struct didn't have the copy, constructor or destructor. I added them latter when I read this post below. http://stackoverflow.com/questions/676575/seg-fault-after-is-item-pushed-onto-stl-container but the end result is the same. There is one this that is bothering me as hell! When I first push some data into the vector, everything goes fine. Later on in the code when I try to push some more data into the vector, my app just segfaults! The vector is declared vector<Group> Groups and is a global variable to the file where I am using it. No externs anywhere else, etc... I can trace the error to: _M_deallocate(this->_M_impl._M_start, this->_M_impl._M_end_of_storage- this->_M_impl._M_start); in vector.tcc when I finish adding/copying the last element to the vector.... As far as I can tell. I shouldn't be needing anything to do with a copy constructor as a shallow copy should be enough for this. I'm not even allocating any space (but I did a reserve for the string to try out). I have no idea what the problem is! I'm running this code on OpenSuse 10.2 with gcc 4.1.2 I'm not really to eager to upgrade gcc because of backward compatibility issues... This code worked "perfectly" on my windows machine. I compiled it with gcc 3.4.5 mingw without any problems... help! --- ... --- :::EDIT::: I push data Group tmp_grp; (...) tmp_grp.name = "Nova "; tmp_grp.codigo=GetGroupnextcode(); tmp_grp.deleted=0; tmp_grp.printers=0; tmp_grp.subpage=0; Groups.push_back(tmp_grp);

    Read the article

  • howto parse struct to C++ dll from C#

    - by Nerds Rule
    I am trying to call a function in a unmanaged C++ dll. It has this prototype: [DllImport("C:\\Program Files\\MySDK\\VSeries.dll", EntryPoint = "BII_Send_Index_Template_MT" )] internal unsafe static extern Int32 BII_Send_Index_Template_MT(IntPtr pUnitHandle, ref BII_Template template, Int32 option, Boolean async); BII_Template template = new BII_Template(); error_code = BII_Send_Index_Template_MT(pUnitHandle, ref template, option, false); I is how I define the BII_Template struct in C#: public unsafe struct BII_Template { public ulong id; public ulong employee_id; public ulong password; public byte sensor_version; public byte template_version; public fixed char name[16]; public byte finger; public byte admin_level; public byte schedule; public byte security_thresh; public fixed byte noise_level[18]; public byte corramb; public byte reference_x; public byte reference_y; public fixed byte ihcore[3]; public fixed byte ivcore[3]; public byte temp_xoffset; public byte temp_yoffset; public byte index; public fixed byte inphase[5500]; }; It build and when I run it the dll return error_code = "The record checksum is invalid." I assume that I am using the ref keyword in a wrong way or the size of some of the elements in the struct is wrong. ----- EDIT ------------ Here is the struct in C++: typedef struct { unsigned long id; unsigned long employee_id; unsigned long password; unsigned char sensor_version; unsigned char template_version; char name[16]; unsigned char finger; unsigned char admin_level; unsigned char schedule; unsigned char security_thresh; unsigned char noise_level[18]; unsigned char corramb ; unsigned char reference_x ; unsigned char reference_y ; unsigned char ihcore[NUM_CORE]; unsigned char ivcore[NUM_CORE]; unsigned char temp_xoffset; unsigned char temp_yoffset; unsigned char index; unsigned char inphase[PACKED_ARRAY_SIZE]; } BII_Template;

    Read the article

  • How to convert struct to char array in C

    - by falcojr
    I'm trying to convert a struct to a char array to send over the network. However, I get some weird output from the char array when I do. #include <stdio.h> struct x { int x; } __attribute__((packed)); int main() { struct x a; a.x=127; char *b = (char *)&a; int i; for (i=0; i<4; i++) printf("%02x ", b[i]); printf("\n"); for (i=0; i<4; i++) printf("%d ", b[i]); printf("\n"); return 0; } Here is the output for various values of a.x (on an X86 using gcc): 127: 7f 00 00 00 127 0 0 0 128: ffffff80 00 00 00 -128 0 0 0 255: ffffffff 00 00 00 -1 0 0 0 256: 00 01 00 00 0 1 0 0 I understand the values for 127 and 256, but why do the numbers change when going to 128? Why wouldn't it just be: 80 00 00 00 128 0 0 0 Am I forgetting to do something in the conversion process or am I forgetting something about integer representation? *Note: This is just a small test program. In a real program I have more in the struct, better variable names, and I convert to little-endian. *Edit: formatting

    Read the article

  • Dynamic memory inside a struct

    - by Maximilien
    Hello, I'm editing a piece of code, that is part of a big project, that uses "const's" to initialize a bunch of arrays. Because I want to parametrize these const's I have to adapt the code to use "malloc" in order to allocate the memory. Unfortunately there is a problem with structs: I'm not able to allocate dynamic memory in the struct itself. Doing it outside would cause to much modification of the original code. Here's a small example: int globalx,globaly; struct bigStruct{ struct subStruct{ double info1; double info2; bool valid; }; double data; //subStruct bar[globalx][globaly]; subStruct ** bar=(subStruct**)malloc(globalx*sizeof(subStruct*)); for(int i=0;i<globalx;i++) bar[i]=(*subStruct)malloc(globaly*sizeof(subStruct)); }; int main(){ globalx=2; globaly=3; bigStruct foo; for(int i=0;i<globalx;i++) for(int j=0;j<globaly;j++){ foo.bar[i][j].info1=i+j; foo.bar[i][j].info2=i*j; foo.bar[i][j].valid=(i==j); } return 0; } Note: in the program code I'm editing globalx and globaly were const's in a specified namespace. Now I removed the "const" so they can act as parameters that are set exactly once. Summarized: How can I properly allocate memory for the substruct inside the struct? Thank you very much! Max

    Read the article

  • How to cast C struct just another struct type if their memory size are equal?

    - by Eonil
    I have 2 matrix structs means equal data but have different form like these: // Matrix type 1. typedef float Scalar; typedef struct { Scalar e[4]; } Vector; typedef struct { Vector e[4]; } Matrix; // Matrix type 2 (you may know this if you're iPhone developer) struct CATransform3D { CGFloat m11, m12, m13, m14; CGFloat m21, m22, m23, m24; CGFloat m31, m32, m33, m34; CGFloat m41, m42, m43, m44; }; typedef struct CATransform3D CATransform3D; Their memory size are equal. So I believe there is a way to convert these types without any pointer operations or copy like this: // Implemented from external lib. CATransform3D CATransform3DMakeScale (CGFloat sx, CGFloat sy, CGFloat sz); Matrix m = (Matrix)CATransform3DMakeScale ( 1, 2, 3 ); Is this possible? Currently compiler prints an "error: conversion to non-scalar type requested" message.

    Read the article

  • How do I convert c struct from dll to C#

    - by poco
    I am converting a application from c++ to C# and have a question about the proper way to handle importing structs. I am attempting to convert a struct from a c dll into c# The struct in c looks like this typedef struct card_info { ushort r; ushort s; enum_a a; usinged long ul; ushort n; ushort* b; ushort id; } CARD_INFO; when i use [StructLayout(LayoutKind.Sequentaial)] the size of the array is 20 bytes in c#. However, if take a look at my working c++ code it is 24 bytes. I changed my c# to look like this: [StructLayout(LayoutKind.Explicit)] public struct CardInfo { [FieldOffset(0) public ushort r; [FieldOffset(2) public ushort s; [FieldOffset(4) public EnumA a; [FieldOffset(8) public ushort ul; [FieldOffset(12) public ushort n; [FieldOffset(16) public UInt32 b; [FieldOffset(20) public ushort id; } This seems to compile but I'm not convinced this is the correct way to go about doing this. Please let me know if this is correct or if there is a better way. Thanks

    Read the article

  • Inserting non-pod struct into a GHashTable

    - by RikSaunderson
    Hi there, I'm trying to build a GHashTable of instances of a struct containing ints, a time_t and a few char*'s. My question is, how do you insert an instance of a struct into a GHashTable? there are plenty of examples of how to insert a string or an int (using g_str_hash and g_int_hash respectively), but I'm guessing thatI want to use the g_direct_hash, and I can't seem to find any examples of that. Ideally, my code would look like this: GHashtable table; table = g_hash_table_new(g_direct_hash, g_direct_equal); struct mystruct; mystruct.a = 1; mystruct.b = "hello"; mystruct.c = 5; mystruct.d = "test"; g_hash_table_insert(table,mystruct.a,mystruct); Clearly, this is incorrect as it does not compile. Can anyone provide an example that does do what I want? Thanks, Rik

    Read the article

  • iPhone/Objective-C struct question my own CGRectZero

    - by Mark
    I am designing a Padding struct as follows: /* Padding. */ struct CGPadding { CGFloat left; CGFloat top; CGFloat right; CGFloat bottom; }; typedef struct CGPadding CGPadding; CG_INLINE CGPadding CGPaddingMake(CGFloat left, CGFloat top, CGFloat right, CGFloat bottom) { CGPadding p; p.left = left; p.top = top; p.right = right; p.bottom = bottom; return p; } This all works perfectly well, the problem is how can I create a const CGPadding CGPaddingZero? If I do like this: const CGPadding CGPaddingZero = (CGPadding){0.0, 0.0, 0.0, 0.0}; It doesnt work. So what am I doing wrong?

    Read the article

< Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >