Search Results

Search found 40165 results on 1607 pages for 'function pointers'.

Page 7/1607 | < Previous Page | 3 4 5 6 7 8 9 10 11 12 13 14  | Next Page >

  • Stucture with array of pointers in C

    - by MVTCplusplus
    What's wrong with this? Can I have an array of pointers to SDL_Surfaces in a struct in C? typedef struct { int next_wheel; int pos_X; int pos_Y; int front_wheel_pos_X; int front_wheel_pos_Y; int velocity; int rear_wheel_pos_X; int rear_wheel_pos_Y; SDL_Surface* body; SDL_Surface* rear_wheel[9]; SDL_Surface* front_wheel[9]; } mars_rover; ... mars_rover* init_rover() { mars_rover* rover = (mars_rover*)malloc(sizeof(mars_rover) + sizeof(SDL_Surface) * 19); ... return rover; } int main() { mars_rover* rover = init_rover(); ... }

    Read the article

  • Structs and pointers

    - by user1763861
    I have a few questions about structs and pointers For this struct: typedef struct tNode_t { char *w; } tNode; How come if I want to change/know the value of *w I need to use t.w = "asdfsd" instead of t->w = "asdfasd"? And I compiled this successfully without having t.w = (char *) malloc(28*sizeof(char)); in my testing code, is there a reason why tt's not needed? Sample main: int main() { tNode t; char w[] = "abcd"; //t.word = (char *) malloc(28*sizeof(char)); t.word = w; printf("%s", t.word); } Thanks.

    Read the article

  • Every function key on laptop works except for brightness

    - by Dave M G
    I have an Asus UX21A, and I'm pleased to discover that almost all the hardware functions and features work perfectly with Ubuntu 12.10. The only thing that is not working straight "out of the box" are the brightness keys. In theory, the screen brightness can be adjusted downward by pressing fn+f5, and adjusted upward by pressing fn+f6. Is there a way I can connect the monitor brightness settings to these function keys?

    Read the article

  • Function pointers in Objective-C

    - by Stefan Klumpp
    I have the following scenario: Class_A - method_U - method_V - method_X - method_Y Class_B - method_M - method_N HttpClass - startRequest - didReceiveResponse // is a callback Now I want to realize these three flows (actually there are many more, but these are enough to demonstrate my question): Class_A :: method_X -> HttpClass :: startRequest:params -> ... wait, wait, wait ... -> HttpClass :: didReceiveResponse -> Class_A :: method_Y:result and: Class_A :: method_U -> HttpClass :: startRequest:params -> ... wait, wait, wait ... -> HttpClass :: didReceiveResponse -> Class_A :: method_V:result and the last one: Class_B :: method_M -> HttpClass :: startRequest:params -> ... wait, wait, wait ... -> HttpClass :: didReceiveResponse -> Class_B :: method_N:result Please note, that the methods in Class_A and Class_B have different names and functionality, they just make us of the same HttpClass. My solution now would be to pass a C function pointer to startRequest, store it in the HttpClass and when didReceiveResponse gets called I invoke the function pointer and pass the result (which will always be a JSON Dictionary). Now I'm wondering if there can be any problems using plain C or if there are better solutions doing it in a more Objective-C way. Any ideas?

    Read the article

  • Dangling pointers and double free

    - by user151410
    After some painful experiences, I understand the problem of dangling pointers and double free. I am seeking proper solutions. aStruct has a number of fields including other arrays. aStruct *A=NULL, *B = NULL; A = (aStruct*) calloc(1, sizeof(sStruct)); B = A; free_aStruct(A); ... //bunch of other code in various places. ... free_aStruct(B); Is there any way to write free_aStruct(X) so that free_aStruct(B) exists gracefully?? void free_aStruct(aStruct *X){ if (X ! = NULL){ if (X->a != NULL){free(X->a); x->a = NULL;} free(X); X = NULL; } } Doing above only sets A = NULL when free_aStruct(A); is called. B is now dangling. How can this situation be avoided / remedied? Is reference counting the only viable solution? or, are there other "defensive" free approaches, to prevent free_aStruct(B); from exploding? Thanks, Russ

    Read the article

  • Trouble with pointers and references in C++

    - by KingNestor
    I have a PolygonList and a Polygon type, which are std::lists of Points or lists of lists of points. class Point { public: int x, y; Point(int x1, int y1) { x = x1; y = y1; } }; typedef std::list<Point> Polygon; typedef std::list<Polygon> PolygonList; // List of all our polygons PolygonList polygonList; However, I'm confused on reference variables and pointers. For example, I would like to be able to reference the first Polygon in my polygonList, and push a new Point to it. So I attempted to set the front of the polygonList to a Polygon called currentPolygon like so: Polygon currentPolygon = polygonList.front(); currentPolygon.push_front(somePoint); and now, I can add points to currentPolygon, but these changes end up not being reflected in that same polygon in the polygonList. Is currentPolygon simply a copy of the Polygon in the front of polygonList? When I later iterate over polygonList all the points I've added to currentPolygon aren't shown. It works if I do this: polygonList.front().push_front(somePoint); Why aren't these the same and how can I create a reference to the physical front polygon rather than a copy of it?

    Read the article

  • Returning pointers in a thread-safe way.

    - by Roddy
    Assume I have a thread-safe collection of Things (call it a ThingList), and I want to add the following function. Thing * ThingList::findByName(string name) { return &item[name]; // or something similar.. } But by doing this, I've delegated the responsibility for thread safety to the calling code, which would have to do something like this: try { list.lock(); // NEEDED FOR THREAD SAFETY Thing *foo = list.findByName("wibble"); foo->Bar = 123; list.unlock(); } catch (...) { list.unlock(); throw; } Obviously a RAII lock/unlock object would simplify/remove the try/catch/unlocks, but it's still easy for the caller to forget. There are a few alternatives I've looked at: Return Thing by value, instead of a pointer - fine unless you need to modify the Thing Add function ThingList::setItemBar(string name, int value) - fine, but these tend to proliferate Return a pointerlike object which locks the list on creation and unlocks it again on destruction. Not sure if this is good/bad practice... What's the right approach to dealing with this?

    Read the article

  • Obj-C: Passing pointers to initialized classes in other classes

    - by FnGreg7
    Hey all. I initialized a class in my singleton called DataModel. Now, from my UIViewController, when I click a button, I have a method that is trying to access that class so that I may add an object to one of its dictionaries. My get/set method passes back the pointer to the class from my singleton, but when I am back in my UIViewController, the class passed back doesn't respond to methods. It's like it's just not there. I think it has something to do with the difference in passing pointers around classes or something. I even tried using the copy method to throw a copy back, but no luck. UIViewController: ApplicationSingleton *applicationSingleton = [[ApplicationSingleton alloc] init]; DataModel *dataModel = [applicationSingleton getDataModel]; [dataModel retrieveDataCategory:dataCategory]; Singleton: ApplicationSingleton *m_instance; DataModel *m_dataModel; - (id) init { NSLog(@"ApplicationSingleton.m initialized."); self = [super init]; if(self != nil) { if(m_instance != nil) { return m_instance; } NSLog(@"Initializing the application singleton."); m_instance = self; m_dataModel = [[DataModel alloc] init]; } NSLog(@"ApplicationSingleton init method returning."); return m_instance; } -(DataModel *)getDataModel { DataModel *dataModel_COPY = [m_dataModel copy]; return dataModel_COPY; } For the getDataModel method, I also tried this: -(DataModel *)getDataModel { return m_dataModel; } In my DataModel retrieveDataCategory method, I couldn't get anything to work. I even just tried putting a NSLog in there but it never would come onto the console. Any ideas?

    Read the article

  • C++ trouble with pointers to objects

    - by Zibd
    I have a class with a vector of pointers to objects. I've introduced some elements on this vector, and on my main file I've managed to print them and add others with no problems. Now I'm trying to remove an element from that vector and check to see if it's not NULL but it is not working. I'm filling it with on class Test: Other *a = new Other(1,1); Other *b = new Other(2,2); Other *c = new Other(3,3); v->push_back(a); v->push_back(b); v->push_back(c); And on my main file I have: Test t; (...) Other *pointer = t.vect->at(0); delete t.vect->at(0); t.vect->erase(t.vect->begin()); if (pointer == NULL) { cout << "Nothing here.."; } // Never enters here..

    Read the article

  • C++ design question, container of instances and pointers

    - by Tom
    Hi all, Im wondering something. I have class Polygon, which composes a vector of Line (another class here) class Polygon { std::vector<Line> lines; public: const_iterator begin() const; const_iterator end() const; } On the other hand, I have a function, that calculates a vector of pointers to lines, and based on those lines, should return a pointer to a Polygon. Polygon* foo(Polygon& p){ std::vector<Line> lines = bar (p.begin(),p.end()); return new Polygon(lines); } Here's the question: I can always add a Polygon (vector Is there a better way that dereferencing each element of the vector and assigning it to the existing vector container? //for line in vector<Line*> v //vcopy is an instance of vector<Line> vcopy.push_back(*(v.at(i)) I think not, but I dont really like that approach. Hopefully, I will be able to convince the author of the class to change it, but I cant base my coding right now to that fact (and i'm scared of a performance hit). Thanks in advance.

    Read the article

  • C++ design question, container of instances and pointers

    - by Tom
    Hi all, Im wondering something. I have class Polygon, which composes a vector of Line (another class here) class Polygon { std::vector<Line> lines; public: const_iterator begin() const; const_iterator end() const; } On the other hand, I have a function, that calculates a vector of pointers to lines, and based on those lines, should return a pointer to a Polygon. Polygon* foo(Polygon& p){ std::vector<Line> lines = bar (p.begin(),p.end()); return new Polygon(lines); } Here's the question: I can always add a Polygon (vector Is there a better way that dereferencing each element of the vector and assigning it to the existing vector container? //for line in vector<Line*> v //vcopy is an instance of vector<Line> vcopy.push_back(*(v.at(i)) I think not, but I dont really like that approach. Hopefully, I will be able to convince the author of the class to change it, but I cant base my coding right now to that fact (and i'm scared of a performance hit). Thanks in advance.

    Read the article

  • im i doing this right or wrong using pointers in C

    - by Amandeep Singh Dhari
    i like to point out that i need some help with my home work, ok the lectuer gave us the idea of a program and we have to make it from bottom to top. got to have user to type in two set of string. pointers take in the value and then puts into a prototype i need to make a 3rd pointer that has the value of p1 and p2. like this p1 = asd, p2 = qwe and p3 = asdqwe #include "stdafx.h" #include <ctype.h> char *mystrcat(char*s1p, char*s2p); // Prototype char main(void) { char string1[80]; char string2[80]; printf("%s", "enter in your srting one "); gets_s(string1); printf("%s", "enter in your srting two "); gets_s(string2); *mystrcat(string1, string2); return 0; } char *mystrcat(char *s1p,char *s2p) { //char *string3; //char *string4; //string3 = s1p; //string4 = s2p; printf("whatever = %s%s\n", s1p, s2p); return 0; } this is the code that i made so far just need some help, thank guys in advance.

    Read the article

  • Is CDS a valid analogy for pointers? [closed]

    - by Flinkman
    So.. bear with me. I just found an analogy to c++ pointers and CDS. This clip describes CDS(Credit Default Swaps). http://www.youtube.com/watch?v=KPNdYtrlgaU#t=120s "Here we know we have an instrument of a particular financial instrument that is demonstrably dangerous, it creates long chains of risk which are vulnerable to the failure of individual trader or market partipants, in that chain and these instruments in an affect permit the creation of vicious spirals. In which the CDS price interact with the bound price, the market price and you can have a downward spiral." What my ears are telling me: "Don't create dependences that will create long chains of crashing systems." Update: Trying to clarify with something that is closer to the readers. If I change the words: instrument = construct financial = language trader = object market partipants = c structs CDS price = uptime bound price = outcome market price = ROI(return on incestment) The quote become more understandable. Look: "Here we know we have construct of a particular language construct that is demonstrably dangerous, it creates long chains of risk which are vulnerable to the failure of individual object or structs in that chain and these system in an affect permit the creation of vicious spirals. In which the uptime interact with the outcome, the ROI and you can have a downward spiral."

    Read the article

  • And now for a complete change of direction from C++ function pointers

    - by David
    I am building a part of a simulator. We are building off of a legacy simulator, but going in different direction, incorporating live bits along side of the simulated bits. The piece I am working on has to, effectively route commands from the central controller to the various bits. In the legacy code, there is a const array populated with an enumerated type. A command comes in, it is looked up in the table, then shipped off to a switch statement keyed by the enumerated type. The type enumeration has a choice VALID_BUT_NOT_SIMULATED, which is effectively a no-op from the point of the sim. I need to turn those no-ops into commands to actual other things [new simulated bits| live bits]. The new stuff and the live stuff have different interfaces than the old stuff [which makes me laugh about the shill job that it took to make it all happen, but that is a topic for a different discussion]. I like the array because it is a very apt description of the live thing this chunk is simulating [latching circuits by row and column]. I thought that I would try to replace the enumerated types in the array with pointers to functions and call them directly. This would be in lieu of the lookup+switch.

    Read the article

  • Passing array of pointers to another class

    - by user310153
    Hi, I am trying to do the following: in main.cpp: // Create an array of pointers to Block objects Block *blk[64]; for (i=0; i<8; i++) { for (j=0; j<8; j++) { int x_low = i*80; int y_low = j*45; blk[j*8+i] = new Block(30, x_low+40.0f, y_low+7.5f, &b); } } And then I am trying to pass it to the graphics object I have created: Graphics g(640, 480, &b, &p, blk[0], number_of_blocks); the graphics constructor looks like: Graphics::Graphics(int width, int height, Ball *b, Paddle *p, Block *blk, int number_of_blocks) { if I look at what is contained in the array from the graphics object, only the first item exists and then all the other items are in hyperspace: for (int i=0; i<64; i++) { printf("for block %d, %f, %f ", i, (_blk+(sizeof(_blk)*i))->_x_low, (_blk+(sizeof(_blk)*i))->_y_low); printf("blah %d\n", (_blk+(sizeof(_blk)*i))); } and if I look at the addresses, they are different (6956552 rather than 2280520 when I examine the addresses in the main class using: printf(" blah %d\n", &blk[j*8*i]); I am sure there must be something subtle I am doing wrong as its like I have copied the first item from the blk array to a new address when passed to the graphics object. Does this make sense? Any ideas? Cheers, Scott

    Read the article

  • Pointers to class fields

    - by newbie_cpp
    My task is as follows : Using pointers to class fields, create menu allowing selection of ice, that Person can buy in Ice shop. Buyer will be charged with waffel and ice costs. Selection of ice and charging buyers account must be shown in program. Here's my Person class : #include <iostream> using namespace std; class Iceshop { const double waffel_price = 1; public: } class Person { static int NUMBER; char* name; int age; const int number; double plus, minus; public: class Account { int number; double resources; public: Account(int number, double resources) : number(number), resources(resources) {} } Person(const char* n, int age) : name(strcpy(new char[strlen(n)+1],n)), number(++NUMBER), plus(0), minus(0), age(age) {} Person::~Person(){ cout << "Destroying resources" << endl; delete [] name; } friend void show(Person &p); int* take_age(){ return &age; } char* take_name(){ return name; } void init(char* n, int a) { name = n; age = a; } Person& remittance(double d) { plus += d; return *this; } Person& paycheck(double d) { minus += d; return *this; } Account* getAccount(); }; int Person:: Person::Account* Person::getAccount() { return new Account(number, plus - minus); } void Person::Account::remittance(double d){ resources = resources + d; } void Person::Account::paycheck(double d){ resources = resources - d; } void show(Person *p){ cout << "Name: " << p->take_name() << "," << "age: " << p->take_age() << endl; } int main(void) { Person *p = new Person; p->init("Mary", 25); show(p); p->remittance(100); system("PAUSE"); return 0; } How to start this task ? Where and in what form should I store menu options ?

    Read the article

  • list of pointers in c++

    - by pavlos
    What i want to do is for (list<cPacket *>::iterator i = cache.begin(); i != cache.end(); i++){ if( strcmp(i->getName(),id) == 0 ){ return true; } } where getName is function of the class cPacket, But it does not work, i tries also i.operator->()->getName(), and again nothing. Can anybody help me?

    Read the article

  • Accessing structure through pointers [c]

    - by Blackbinary
    I've got a structure which holds names and ages. I've made a linked-list of these structures, using this as a pointer: aNode *rootA; in my main. Now i send **rootA to a function like so addElement(5,"Drew",**rootA); Because i need to pass rootA by reference so that I can edit it in other functions (in my actual program i have two roots, so return will not work) The problem is, in my program, i can't say access the structure members. *rootA->age = 4; for example doesnt work. Hopefully you guys can help me out. Thanks!

    Read the article

  • Storing member function pointers of derived classes in map

    - by Kiran Mohan
    Hello, I am trying to implement a factory for two classes Circle, Square both of which inherits from Shape. class Shape { public: virtual static Shape * getInstance() = 0; }; class Circle : public Shape { public: static const std::string type; Shape * getInstance() { return new Circle; } }; const std::string Circle::type = "Circle"; class Square : public Shape { public: static const std::string type; Shape * getInstance() { return new Square; } }; const std::string Square::type = "Square"; I want to now create a map with key as shape type (string) and value as a function pointer to getInstance() of the corresponding derived class. Is it possible? Thanks, Kiran

    Read the article

  • Passing array to function with pointer loses array size information!

    - by Narek
    If I write int main() { int a[100] = {1,2,3,4,}; cout<<sizeof(a)/sizeof(a[0])<<endl; return 0; } I get 400! If I write void func(int *a); int main() { int a[100] = {1,2,3,4,}; func(a); return 0; } void func(int *a) { cout<<sizeof(a)/sizeof(a[0])<<endl; } Then I get 400! So why passing array to function with pointer loses array size information?

    Read the article

  • How to use pointers and pointer aritmetic

    - by booby
    : error C2064: term does not evaluate to a function taking 1 arguments : error C2227: left of '-name' must point to class/struct/union/generic type how do i fix this so this error doesn't happen for(int index = 0; index < (numStudents); index++) { if (student(index + 1)->score >= 90 ) student(index + 1)->grade = 'A'; else if (student(index + 1)->score >= 80 ) student(index + 1)->grade = 'B'; else if (student(index + 1)->score >= 70 ) student(index + 1)->grade = 'C'; else if (student(index + 1)->score >= 60 ) student(index + 1)->grade = 'D'; else student(index + 1)->grade = 'F'; }

    Read the article

  • C pointers and addresses

    - by yCalleecharan
    Hi, I always thought that *&p = p = &*p in C. I tried this code: #include <stdio.h> #include <stdlib.h> char a[] = "programming"; char *ap = &a[4]; int main(void) { printf("%x %x %x\n", ap, &*(ap), *&(ap)); /* line 13 */ printf("%x %x %x\n\n", ap+1, &*(ap+1), *&(ap+1)); /* line 14 */ } The first printf line (line 13) gives me the addresses: 40b0a8 40b0a8 40b0a8 which are the same as expected. But when I added the second printf line, Borland complains: "first.c": E2027 Must take address of a memory location in function main at line 14 I was expecting to get: 40b0a9 40b0a9 40b0a9. It seems that the expression *&(ap+1) on line 14 is the culprit here. I thought all three pointer expressions on line 14 are equivalent. Why am I thinking wrong? A second related question: The line char *ap = a; points to the first element of array a. I used char *ap = &a[4]; to point to the 5th element of array a. Is the expression char *ap = a; same as the expression char *ap = &a[0]; Is the last expression only more verbose than the previous one? Thanks a lot...

    Read the article

  • What does it mean to pass a &variable to a function? E.g., string& insert ( size_t pos1, const strin

    - by Bob Montgomery
    I understand passing a pointer, and returning a pointer: char * strcat ( char * destination, const char * source ); You're passing a variable that contains the address to a char; returning the same. But what does it mean to pass something using the reference operator? Or to return it? string& insert ( size_t pos1, const string& str ); I mean, I understand what actually happens, I just don't understand the notation. Why isn't the notation this instead: string * insert ( size_t pos1, const string * str ); //made up I presume it has something to do with passing/returning the instance of a class, but what? Is this syntax valid; if not why not and if so what does it mean? char & strcat ( char & destination, const char & source ); //made up (all of the function declarations, except the last made-up two, are from http://www.cplusplus.com )

    Read the article

  • Start a thread using a method pointer

    - by Michael
    Hi ! I'm trying to develop a thread abstraction (POSIX thread and thread from the Windows API), and I would very much like it to be able to start them with a method pointer, and not a function pointer. What I would like to do is an abstraction of thread being a class with a pure virtual method "runThread", which would be implanted in the future threaded class. I don't know yet about the Windows thread, but to start a POSIX thread, you need a function pointer, and not a method pointer. And I can't manage to find a way to associate a method with an instance so it could work as a function. I probably just can't find the keywords (and I've been searching a lot), I think it's pretty much what Boost::Bind() does, so it must exist. Can you help me ?

    Read the article

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