Search Results

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

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

  • Printing the address of a struct object

    - by bdhar
    I have a struct like this typedef struct _somestruct { int a; int b; }SOMESTRUCT,*LPSOMESTRUCT; I am creating an object for the struct and trying to print it's address like this int main() { LPSOMESTRUCT val = (LPSOMESTRUCT)malloc(sizeof(SOMESTRUCT)); printf("0%x\n", val); return 0; } ..and I get this warning warning C4313: 'printf' : '%x' in format string conflicts with argument 1 of type 'LPSOMESTRUCT' So, I tried to cast the address to int like this printf("0%x\n", static_cast<int>(val)); But I get this error: error C2440: 'static_cast' : cannot convert from 'LPSOMESTRUCT' to 'int' What am I missing here? How to avoid this warning? Thanks.

    Read the article

  • How to check if the internal typedef struct of a typedef struct is NULL ?

    - by watchloop
    typedef struct { uint32 item1; uint32 item2; uint32 item3; uint32 item4; <some_other_typedef struct> *table; } Inner_t; typedef struct { Inner_t tableA; Inner_t tableB; } Outer_t; Outer_t outer_instance = { {NULL}, { 0, 1, 2, 3, table_defined_somewhere_else, } }; My question is how to check if tableA is NULL just like the case for outer_instance. It tried: if ( tmp->tableA == NULL ). I get "error: invalid operands to binary =="

    Read the article

  • Define and return a struct in c

    - by nevan
    I'm trying to convert some code from Javascript to c. The function creates an array (which always has a fixed number of items) and then returns the array. I've learned that in c it's not straightforward to return an array, so I'd like to return this as a struct instead. My c is not all that great, so I'd like to check that returning a struct is the right thing to do in this situation, and that I'm doing it the right way. Thanks. typedef struct { double x; double y; double z; } Xyz; Xyz xyzPlusOne(Xyz addOne) { Xyz xyz; xyz.x = addOne.x + 1; xyz.y = addOne.y + 1; xyz.z = addOne.z + 1; return xyz; }

    Read the article

  • struct with template variables in c++

    - by monkeyking
    I'm playing around with template, so I'm not trying to reinvent the std::vector, I'm trying to get a grasp of templateting in c++. Can I do the following template <typename T> typedef struct{ size_t x; T *ary; }array; What I'm trying to do is a basic templated version of typedef struct{ size_t x; int *ary; }iArray; It looks like its working if I use a class instead of struct, so is it not possible with typedef structs? thanks

    Read the article

  • Static initialization of a struct with class members

    - by JS Bangs
    I have a struct that's defined with a large number of vanilla char* pointers, but also an object member. When I try to statically initialize such a struct, I get a compiler error. typedef struct { const char* pszA; // ... snip ... const char* pszZ; SomeObject obj; } example_struct; // I only want to assign the first few members, the rest should be default example_struct ex = { "a", "b" }; SomeObject has a public default constructor with no arguments, so I didn't think this would be a problem. But when I try to compile this (using VS), I get the following error: error C2248: 'SomeObject::SomeObject' : cannot access private member declared in class 'SomeObject' Any idea why?

    Read the article

  • C++ vector and struct problem win32

    - by ~james2432
    I have a structure defined in my header file: struct video { wchar_t* videoName; std::vector<wchar_t*> audio; std::vector<wchar_t*> subs; }; struct ret { std::vector<video*> videos; wchar_t* errMessage; }; struct params{ HWND form; wchar_t* cwd; wchar_t* disk; ret* returnData; }; When I try to add my video structure to a vector of video* I get access violation reading 0xcdcdcdc1 (videoName is @ 0xcdcdcdcd, before I allocate it) //extract of code where problem is video v; v.videoName = (wchar_t*)malloc((wcslen(line)+1)*sizeof(wchar_t)); wcscpy(v.videoName,line); p->returnData->videos.push_back(&v); //error here

    Read the article

  • Ojective C Class or struct?

    - by Scott Pendleton
    I have a class Song with properties Title, Key, Artist, etc. There are no methods. I loop through a database of song information and create a Song object for each, populating the properties, and then store the Song objects in an NSArray. Then I thought, why not just have a struct Song with all those same properties instead of a class Song. Doing so would eliminate the class files, the #import Song line in the using class's .m file, and the need to alloc, init, release. On the other hand, I'd have to put the struct definition in every class that might need it. (Unless there's some globally accessible location -- is there?) Also, can a struct be stored in an NSArray?

    Read the article

  • Returning a struct from a class method

    - by tree
    I have a header file that looks something like the following: class Model { private: struct coord { int x; int y; } xy; public: .... coord get() const { return xy; } }; And in yet another file (assume ModelObject exists): struct c { int x; int y; void operator = (c &rhs) { x = rhs.x; y = rhs.y; }; } xy; xy = ModelObject->get(); The compiler throws an error that says there is no known covnersion from coord to c. I believe it is because it doesn't know about coord type because it is declared inside of a class header. I can get around that by declaring the struct outside of the class, but I was wondering if it is possible to do the way I am, or is this generally considered bad practice

    Read the article

  • Sizeof struct in GO

    - by Homer J. Simpson
    I'm having a look at Go, which looks quite promising. I am trying to figure out how to get the size of a go struct, for example something like type Coord3d struct { X, Y, Z int64 } Of course I know that it's 24 bytes, but I'd like to know it programmatically.. Do you have any ideas how to do this ?

    Read the article

  • How to move a struct into a class?

    - by Kache4
    I've got something like: typedef struct Data_s { int field1; int field2; } Data; class Foo { void useData(Data& data); } Is there a way to move the struct into the class without creating a new class? Currently, just pasting it inside the class and replacing Data with Foo::Data everywhere doesn't work.

    Read the article

  • When to use a namespace or a struct?

    - by Justen
    I was just reading a little bit on them from http://www.cplusplus.com/doc/tutorial/namespaces/ and it seems like a struct is capable of the same things? Or even a class for that matter. Maybe someone here can better define what a namespace is, and how it differs from a struct/class?

    Read the article

  • Struct with pointer to a function

    - by user354021
    Hello, In a C struct I have defined a function pointer as follows: typedef struct _sequence_t { const int seq[3]; typedef void (* callbackPtr)(); } sequence_t; I want to initialize a var of that type globally with: sequence_t sequences[] = { { { 0, 1, 2 }, toggleArmament }, }; And I keep getting error telling me that there are too many initializers. How to work it out?

    Read the article

  • C Programming. How to deep copy a struct?

    - by user69514
    I have the following two structs where "child struct" has a "rusage struct" as an element. Then I create two structs of type "child" let's call them childA and childB How do I copy just the rusage struct from childA to childB? typedef struct{ int numb; char *name; pid_t pid; long userT; long systemT; struct rusage usage; }child; typedef struct{ struct timeval ru_utime; /* user time used */ struct timeval ru_stime; /* system time used */ long ru_maxrss; /* maximum resident set size */ long ru_ixrss; /* integral shared memory size */ long ru_idrss; /* integral unshared data size */ long ru_isrss; /* integral unshared stack size */ long ru_minflt; /* page reclaims */ long ru_majflt; /* page faults */ long ru_nswap; /* swaps */ long ru_inblock; /* block input operations */ long ru_oublock; /* block output operations */ long ru_msgsnd; /* messages sent */ long ru_msgrcv; /* messages received */ long ru_nsignals; /* signals received */ long ru_nvcsw; /* voluntary context switches */ long ru_nivcsw; /* involuntary context switches */ }rusage; I did the following, but I guess it copies the memory location, because if I changed the value of usage in childA, it also changes in childB. memcpy(&childA,&childB, sizeof(rusage)); I know that gives childB all the values from childA. I have already taken care of the others fields in childB, I just need to be able to copy the rusage struct called usage that resides in the "child" struct.

    Read the article

  • Hide struct definition in static library.

    - by BobMcLaury
    Hi, I need to provide a C static library to the client and need to be able to make a struct definition unavailable. On top of that I need to be able to execute code before the main at library initialization using a global variable. Here's my code: private.h #ifndef PRIVATE_H #define PRIVATE_H typedef struct TEST test; #endif private.c (this should end up in a static library) #include "private.h" #include <stdio.h> struct TEST { TEST() { printf("Execute before main and have to be unavailable to the user.\n"); } int a; // Can be modified by the user int b; // Can be modified by the user int c; // Can be modified by the user } TEST; main.c test t; int main( void ) { t.a = 0; t.b = 0; t.c = 0; return 0; } Obviously this code doesn't work... but show what I need to do... Anybody knows how to make this work? I google quite a bit but can't find an answer, any help would be greatly appreciated. TIA!

    Read the article

  • A public struct inside a class

    - by Koning Baard
    I am new to C++, and let's say I have two classes: Creature and Human: /* creature.h */ class Creature { private: public: struct emotion { /* All emotions are percentages */ char joy; char trust; char fear; char surprise; char sadness; char disgust; char anger; char anticipation; char love; }; }; /* human.h */ class Human : Creature { }; And I have this in my main function in main.cpp: Human foo; My question is: how can I set foo's emotions? I tried this: foo->emotion.fear = 5; But GCC gives me this compile error: error: base operand of '-' has non-pointer type 'Human' This: foo.emotion.fear = 5; Gives: error: 'struct Creature::emotion' is inaccessible error: within this context error: invalid use of 'struct Creature::emotion' Can anyone help me? Thanks P.S. No I did not forget the #includes

    Read the article

  • typedef struct, circular dependency, forward definitions

    - by BlueChip
    The problem I have is a circular dependency issue in C header files ...Having looked around I suspect the solution will have something to do with Forward Definitions, but although there are many similar problems listed, none seem to offer the information I require to resolve this one... I have the following 5 source files: // fwd1.h #ifndef __FWD1_H #define __FWD1_H #include "fwd2.h" typedef struct Fwd1 { Fwd2 *f; } Fwd1; void fwd1 (Fwd1 *f1, Fwd2 *f2) ; #endif // __FWD1_H . // fwd1.c #include "fwd1.h" #include "fwd2.h" void fwd1 (Fwd1 *f1, Fwd2 *f2) { return; } . // fwd2.h #ifndef __FWD2_H #define __FWD2_H #include "fwd1.h" typedef struct Fwd2 { Fwd1 *f; } Fwd2; void fwd2 (Fwd1 *f1, Fwd2 *f2) ; #endif // __FWD2_H . // fwd2.c #include "fwd1.h" #include "fwd2.h" void fwd2 (Fwd1 *f1, Fwd2 *f2) { return; } . // fwdMain.c #include "fwd1.h" #include "fwd2.h" int main (int argc, char** argv, char** env) { Fwd1 *f1 = (Fwd1*)0; Fwd2 *f2 = (Fwd2*)0; fwd1(f1, f2); fwd2(f1, f2); return 0; } Which I am compiling with the command: gcc fwdMain.c fwd1.c fwd2.c -o fwd -Wall I have tried several ideas to resolve the compile errors, but have only managed to replace the errors with other errors ...How do I resolve the circular dependency issue with the least changes to my code? ...Ideally, as a matter of coding style, I would like to avoid putting the word "struct" all over my code.

    Read the article

  • Struct arrays in C

    - by ThomasTheTankEngine
    Hi I'm having trouble trying to initializing each element of the struct array. When I try and assign the value ZERO to both 'bSize' and 'msgs', it doesn't work as it errors out when i get to malloc. In the printf statement it prints a -1852803823 number. Excuse the messy code as i'm playing around trying to figure it out. struct message{ int *data; int bSize; int msgs; }; int main(int argc, char *argv[]) { ..... } void getSchedFile (FILE *file, int **schd) { struct message sMsg[nodeCount]; const int pakSize = 6; // Iniitialise message buffer for (int i=0; i<nodeCount; i++){ sMsg[i].bSize = 0; sMsg[i].msgs = 0; printf("bSize %d\n",sMsg[i].bSize); } /* Get the number of bytes */ fseek(file, 0L, SEEK_SET); int time; while((fscanf(file, "%d", &time)) != EOF){ int src; fscanf(file, "%d", &src); // get source node id // These are here for easier reading code int aPos = sMsg[src].bSize; int nMsg = sMsg[src].msgs; printf("size %d\n", sMsg[src].bSize); if (sMsg[src].bSize==0){ sMsg[src].data = malloc( pakSize * sizeof(int)); }else{ sMsg[src].data = realloc(sMsg[src].data, (aPos+pakSize)*sizeof(int)); }

    Read the article

  • How to have struct members accessible in different ways

    - by Paul J. Lucas
    I want to have a structure token that has start/end pairs for position, sentence, and paragraph information. I also want the members to be accessible in two different ways: as a start/end pair and individually. Given: struct token { struct start_end { int start; int end; }; start_end pos; start_end sent; start_end para; typedef start_end token::*start_end_ptr; }; I can write a function, say distance(), that computes the distance between any of the three start/end pairs like: int distance( token const &i, token const &j, token::start_end_ptr mbr ) { return (j.*mbr).start - (i.*mbr).end; } and call it like: token i, j; int d = distance( i, j, &token::pos ); that will return the distance of the pos pair. But I can also pass &token::sent or &token::para and it does what I want. Hence, the function is flexible. However, now I also want to write a function, say max(), that computes the maximum value of all the pos.start or all the pos.end or all the sent.start, etc. If I add: typedef int token::start_end::*int_ptr; I can write the function like: int max( list<token> const &l, token::int_ptr p ) { int m = numeric_limits<int>::min(); for ( list<token>::const_iterator i = l.begin(); i != l.end(); ++i ) { int n = (*i).pos.*p; // NOT WHAT I WANT: It hard-codes 'pos' if ( n > m ) m = n; } return m; } and call it like: list<token> l; l.push_back( i ); l.push_back( j ); int m = max( l, &token::start_end::start ); However, as indicated in the comment above, I do not want to hard-code pos. I want the flexibility of accessible the start or end of any of pos, sent, or para that will be passed as a parameter to max(). I've tried several things to get this to work (tried using unions, anonymous unions, etc.) but I can't come up with a data structure that allows the flexibility both ways while having each value stored only once. Any ideas how to organize the token struct so I can have what I want? Attempt at clarification Given struct of pairs of integers, I want to be able to "slice" the data in two distinct ways: By passing a pointer-to-member of a particular start/end pair so that the called function operates on any pair without knowing which pair. The caller decides which pair. By passing a pointer-to-member of a particular int (i.e., only one int of any pair) so that the called function operates on any int without knowing either which int or which pair said int is from. The caller decides which int of which pair. Another example for the latter would be to sum, say, all para.end or all sent.start. Also, and importantly: for #2 above, I'd ideally like to pass only a single pointer-to-member to reduce the burden on the caller. Hence, me trying to figure something out using unions.

    Read the article

  • C - struct problems - writing

    - by Catarrunas
    Hello, I'm making a program in C, and I'mm having some troubles with memory, I think. So my problem is: I have 2 functions that return a struct. When I run only one function at a time I have no problem whatsoever. But when I run one after the other I always get an error when writting to the second struct. Function struct item* ReadFileBIN(char *name) -- reads a binary file. struct tables* getMesasInfo(char* Filename) -- reads a text file. My code is this: #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> int numberOfTables=0; int numberOfItems=0; //struct tables* mesas; //struct item* Menu; typedef struct item{ char nome[100]; int id; float preco; }; typedef struct tables{ int id; int capacity; bool inUse; }; struct tables* getMesasInfo(char* Filename){ struct tables* mesas; char *c; int counter,numberOflines=0,temp=0; char *filename=Filename; FILE * G; G = fopen(filename,"r"); if (G==NULL){ printf("Cannot open file.\n"); } else{ while (!feof(G)){ fscanf(G, "%s", &c); numberOflines++; } fclose(G); } /* Memory allocate for input array */ mesas = (struct tables *)malloc(numberOflines* sizeof(struct tables*)); counter=0; G=fopen(filename,"r"); while (!feof(G)){ mesas[counter].id=counter; fscanf(G, "%d", &mesas[counter].capacity); mesas[counter].inUse= false; counter++; } fclose(G); numberOfTables = counter; return mesas; } struct item* ReadFileBIN(char *name) { int total=0; int counter; FILE *ptr_myfile; struct item my_record; struct item* Menu; ptr_myfile=fopen(name,"r"); if (!ptr_myfile) { printf("Unable to open file!"); } while (!feof(ptr_myfile)){ fread(&my_record,sizeof(struct item),1,ptr_myfile); total=total+1; } numberOfItems=total-1; Menu = (struct item *)calloc(numberOfItems , sizeof(struct item)); fseek(ptr_myfile, sizeof(struct item), SEEK_END); rewind(ptr_myfile); for ( counter=1; counter < total ; counter++) { fread(&my_record,sizeof(struct item),1,ptr_myfile); Menu[counter] = my_record; printf("Nome: %s\n",Menu[counter].nome); printf("ID: %d\n",Menu[counter].id); printf("Preco: %f\n",Menu[counter].preco); } fclose(ptr_myfile); return Menu; } int _tmain(int argc, _TCHAR* argv[]) { struct item* tt = ReadFileBIN("menu.dat"); struct tables* t = getMesasInfo("Capacity.txt"); getchar(); }** Thanks in advance.

    Read the article

  • Enumerator Implementation: Use struct or class?

    - by Hosam Aly
    I noticed that List<T> defines its enumerator as a struct, while ArrayList defines its enumerator as a class. What's the difference? If I am to write an enumerator for my class, which one would be preferable? EDIT: My requirements cannot be fulfilled using yield, so I'm implementing an enumerator of my own. That said, I wonder whether it would be better to follow the lines of List<T> and implement it as a struct.

    Read the article

  • Racket list in struct

    - by Tim
    I just started programming with Racket and now I have the following problem. I have a struct with a list and I have to add up all prices in the list. (define-struct item (name category price)) (define some-items (list (make-item "Book1" 'Book 40.97) (make-item "Book2" 'Book 5.99) (make-item "Book3" 'Book 20.60) (make-item "Item" 'KitchenAccessory 2669.90))) I know that I can return the price with: (item-price (first some-items)) or (item-price (car some-items)). The problem is, that I dont know how I can add up all Items prices with this. Answer to Óscar López: May i filled the blanks not correctly, but Racket mark the code black when I press start and don't return anything. (define (add-prices items) (if (null? items) (+ 0 items) ; Here I don't really know what to write for a 0. ; I tried differnt thnigs like null and this version. (+ (item-price (first some-items)) (add-prices (item-price (rest some-items))))))

    Read the article

  • struct assignment operator on arrays

    - by Django fan
    Suppose I defined a structure like this: struct person { char name [10]; int age; }; and declared two person variables: person Bob; person John; where Bob.name = "Bob", Bob.age = 30 and John.name = "John",John.age = 25. and I called Bob = John; struct person would do a Memberwise assignment and assign Johns's member values to Bob's. But arrays can't assign to arrays, so how does the assignment of the "name" array work?

    Read the article

  • Struct in C, are they efficient?

    - by pygabriel
    I'm reading some C code like that: double function( int lena,double xa,double ya, double za, double *acoefs, ..., int lenb,double xb,double yb, double zb, double *bcoefs, ..., same for c, same for d ) This function is called in the code mor than 100.000 times so it's performance-critical. I'm trying to extend this code but I want to know if it's efficient or not (and how much this influences the speed) to encapsulate all the parameters in a struct like this struct PGTO { int len; double x,y,z ; double *acoefs } and then access the parameters in the function.

    Read the article

  • Port C's fread(&struct,....) to Python

    - by user287669
    Hey, I'm really struggling with this one. I'am trying to port a small piece of someone else's code to Python and this is what I have: typedef struct { uint8_t Y[LUMA_HEIGHT][LUMA_WIDTH]; uint8_t Cb[CHROMA_HEIGHT][CHROMA_WIDTH]; uint8_t Cr[CHROMA_HEIGHT][CHROMA_WIDTH]; } __attribute__((__packed__)) frame_t; frame_t frame; while (! feof(stdin)) { fread(&frame, 1, sizeof(frame), stdin); // DO SOME STUFF } Later I need to access the data like so: frame.Y[x][y] So I made a Class 'frame' in Python and inserted the corresponding variables(frame.Y, frame.Cb, frame.Cr). I have tried to sequentially map the data from Y[0][0] to Cr[MAX][MAX], even printed out the C struct in action but didn't manage to wrap my head around the method used to put the data in there. I've been struggling overnight with this and have to get back to the army tonight, so any immediate help is very welcome and appreciated. Thanks

    Read the article

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