Search Results

Search found 3255 results on 131 pages for 'pointers'.

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

  • 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

  • C++ template and pointers

    - by Kary
    I have a problem with a template and pointers ( I think ). Below is the part of my code: /* ItemCollection.h */ #ifndef ITEMCOLLECTION_H #define ITEMCOLLECTION_H #include <cstddef> using namespace std; template <class T> class ItemCollection { public: // constructor //destructor void insertItem( const T ); private: struct Item { T price; Item* left; Item* right; }; Item* root; Item* insert( T, Item* ); }; #endif And the file with function defintion: /* ItemCollectionTemp.h-member functions defintion */ #include <iostream> #include <cstddef> #include "ItemCollection.h" template <class Type> Item* ItemCollection <T>::insert( T p, Item* ptr) { // function body } Here are the errors which are generated by this line of code: Item* ItemCollection <T>::insert( T p, Item* ptr) Errors: error C2143: syntax error : missing ';' before '*' error C4430: missing type specifier - int assumed. Note: C++ does not support default-int error C2065: 'Type' : undeclared identifier error C2065: 'Type' : undeclared identifier error C2146: syntax error : missing ')' before identifier 'p' error C4430: missing type specifier - int assumed. Note: C++ does not support default-int error C2470: 'ItemCollection::insert' : looks like a function definition, but there is no parameter list; skipping apparent body error C2072: 'ItemCollection::insert': initialization of a function error C2059: syntax error : ')' Any help is much appreciated.

    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

  • Automatically generate table of function pointers in C.

    - by jeremytrimble
    I'm looking for a way to automatically (as part of the compilation/build process) generate a "table" of function pointers in C. Specifically, I want to generate an array of structures something like: typedef struct { void (*p_func)(void); char * funcName; } funcRecord; /* Automatically generate the lines below: */ extern void func1(void); extern void func2(void); /* ... */ funcRecord funcTable[] = { { .p_func = &func1, .funcName = "func1" }, { .p_func = &func2, .funcName = "func2" } /* ... */ }; /* End automatically-generated code. */ ...where func1 and func2 are defined in other source files. So, given a set of source files, each of which which contain a single function that takes no arguments and returns void, how would one automatically (as part of the build process) generate an array like the one above that contains each of the functions from the files? I'd like to be able to add new files and have them automatically inserted into the table when I re-compile. I realize that this probably isn't achievable using the C language or preprocessor alone, so consider any common *nix-style tools fair game (e.g. make, perl, shell scripts (if you have to)). But Why? You're probably wondering why anyone would want to do this. I'm creating a small test framework for a library of common mathematical routines. Under this framework, there will be many small "test cases," each of which has only a few lines of code that will exercise each math function. I'd like each test case to live in its own source file as a short function. All of the test cases will get built into a single executable, and the test case(s) to be run can be specified on the command line when invoking the executable. The main() function will search through the table and, if it finds a match, jump to the test case function. Automating the process of building up the "catalog" of test cases ensures that test cases don't get left out (for instance, because someone forgets to add it to the table) and makes it very simple for maintainers to add new test cases in the future (just create a new source file in the correct directory, for instance). Hopefully someone out there has done something like this before. Thanks, StackOverflow community!

    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

  • Problem with pointers and getstring function

    - by volting
    I am trying to write a function to get a string from the uart1. Its for an embedded system so I don't want to use malloc. The pointer that is passed to the getstring function seems to point to garbage after the gets_e_uart1() is called. I don't use pointers too often so I'm sure it is something really stupid and trivial that Im doing wrong. Regards, V int main() { char *ptr = 0; while(1) { gets_e_uart1(ptr, 100); puts_uart1(ptr); } return 0; }*end main*/ //------------------------------------------------------------------------- //gets a string and echos it //returns 0 if there is no error char getstring_e_uart1(char *stringPtr_, const int SIZE_) { char buffer_[SIZE_]; stringPtr_ = buffer_; int start_ = 0, end_ = SIZE_ - 1; char errorflag = 0; /*keep geting chars until newline char recieved*/ while((buffer_[start_++] = getchar_uart1())!= 0x0D) { putchar_uart1(buffer_[start_]);//echo it /*check for end of buffer wraparound if neccesary*/ if(start_ == end_) { start_ = 0; errorflag = 1; } } putchar_uart1('\n'); putchar_uart1('\r'); /*check for end of buffer wraparound if neccesary*/ if(start_ == end_) { buffer_[0] = '\0'; errorflag = 1; } else { buffer_[start_++] = '\0'; } return errorflag; } Update: I decided to go with approach of passing a pointer an array to the function. This works nicely, thanks to everyone for the informative answers. Updated Code: //------------------------------------------------------------------------- //argument 1 should be a pointer to an array, //and the second argument should be the size of the array //gets a string and echos it //returns 0 if there is no error char getstring_e_uart1(char *stringPtr_, const int SIZE_) { char *startPtr_ = stringPtr_; char *endPtr_ = startPtr_ + (SIZE_ - 1); char errorflag = 0; /*keep geting chars until newline char recieved*/ while((*stringPtr_ = getchar_uart1())!= 0x0D) { putchar_uart1(*stringPtr_);//echo it stringPtr_++; /*check for end of buffer wraparound if neccesary*/ if(stringPtr_ == endPtr_) { stringPtr_ = startPtr_; errorflag = 1; } } putchar_uart1('\n'); putchar_uart1('\r'); /*check for end of buffer wraparound if neccesary*/ if(stringPtr_ == endPtr_) { stringPtr_ = startPtr_; *stringPtr_ = '\0'; errorflag = 1; } else { *stringPtr_ = '\0'; } return errorflag; }

    Read the article

  • not so obvious pointers

    - by mike_hornbeck
    I have a class : class X{ public : void f ( int ) ; int a ; } ; And the task is "Inside the code provide declarations for : pointer to int variable of class X pointer to function void(int) defined inside class X pointer to double variable of class X" Ok so pointer to int a will be just int *x = &a, right ? If there is no double in X can I already create pointer to double inside this class ? And the biggest problem is the second task. How one declares pointer to function ?

    Read the article

  • Void pointers in C

    - by avd
    I have written this qsort: void qsort(void *a[],int low,int high, int (*compare)(void*,void*)); When I call this on char *strarr[5]; It says invalid conversion from char** to void**. Why this is wrong? This is the code:http://codepad.org/Flfm2zAE

    Read the article

  • C++ Pointers, objects, etc

    - by Zeee
    It may be a bit confusing, but... Let's say I have a vector type in a class to store objects, something like vector, and I have methods on my class that will later return Operators from this vector. Now if any of my methods receives an Operator, will I have any trouble to insert it directly into the vector? Or should I use the copy constructor to create a new Operator and put this new one on the vector?

    Read the article

  • Generic function pointers in C

    - by Lucas
    I have a function which takes a block of data and the size of the block and a function pointer as argument. Then it iterates over the data and performes a calculation on each element of the data block. The following is the essential outline of what I am doing: int myfunction(int* data, int size, int (*functionAsPointer)(int)){ //walking through the data and calculating something for (int n = 0; n < size; n++){ data[n] = (*function)(data[n]); } } The functions I am passing as arguments look something like this: int mycalculation(int input){ //doing some math with input //... return input; } This is working well, but now I need to pass an additional variable to my functionpointer. Something along the lines int mynewcalculation(int input, int someVariable){ //e.g. input = input * someVariable; //... return input; } Is there an elegant way to achieve this and at the same time keeping my overall design idea?

    Read the article

  • array of pointers

    - by tushar
    char *a[]={"diamonds","clubs","spades","hearts"}; char **p[]={a+3,a+2,a+1,a}; char ***ptr=p; cout<<*ptr[2][2]; why does it display h and please explain how is the 2d array of ptr implementing and its elements

    Read the article

  • Vector of pointers to base class, odd behaviour calling virtual functions

    - by Ink-Jet
    I have the following code #include <iostream> #include <vector> class Entity { public: virtual void func() = 0; }; class Monster : public Entity { public: void func(); }; void Monster::func() { std::cout << "I AM A MONSTER" << std::endl; } class Buddha : public Entity { public: void func(); }; void Buddha::func() { std::cout << "OHMM" << std::endl; } int main() { const int num = 5; // How many of each to make std::vector<Entity*> t; for(int i = 0; i < num; i++) { Monster m; Entity * e; e = &m; t.push_back(e); } for(int i = 0; i < num; i++) { Buddha b; Entity * e; e = &b; t.push_back(e); } for(int i = 0; i < t.size(); i++) { t[i]->func(); } return 0; } However, when I run it, instead of each class printing out its own message, they all print the "Buddha" message. I want each object to print its own message: Monsters print the monster message, Buddhas print the Buddha message. What have I done wrong?

    Read the article

  • Member variable pointers to COM objects

    - by drelihan
    Hi Folks, Is there any problem with keeping member variable pointer refernces to COM objects and reussing the reference through out the class in C++. Is anybody aware of a reason why you would want to call .CreateInstance every time you wanted a to use the COM object i.e. you were getting a fresh instance each time. I cannot see any reason who you would want to do this, Thanks, (No is an acceptable answer!!!)

    Read the article

  • Accessing structure elements using pointers

    - by Arun Nadesh
    Hi Everybody, Greetings! I got surprised when the following program did not crash. typedef struct _x{ int a; char b; int c; }x; main() { x *ptr=0; char *d=&ptr->b; } As per my understanding the -> operator has higher precedence over & operator. So I expected the program to crash at the below statement when we try to dereference the NULL pointer tr. char *d=&ptr->b; But the statement &ptr->b evaluates to a valid address. Could somebody please explain where I'm wrong? Thanks & Regards, Arun

    Read the article

  • Arrays of pointers to arrays?

    - by a2h
    I'm using a library which for one certain feature involves variables like so: extern const u8 foo[]; extern const u8 bar[]; I am not allowed to rename these variables in any way. However, I like to be able to access these variables through an array (or other similar method) so that I do not need to continually hardcode new instances of these variables into my main code. My first attempt at creating an array is as follows: const u8* pl[] = { &foo, &bar }; This gave me the error cannot convert 'const u8 (*)[]' to 'const u8*' in initialization, and with help elsewhere along with some Googling, I changed my array to this: u8 (*pl)[] = { &foo, &bar }; Upon compiling I now get the error scalar object 'pl' requires one element in initializer. Does anyone have any ideas on what I'm doing wrong? Thanks.

    Read the article

  • How to handle an array of pointers in Objective-C

    - by DougW
    I figured out the answer to this question, but I couldn't find the solution on here, so posting it for posterity. So, in Objective-C, how do you create an object out of a pointer in order to store it in objective-c collections (NSArray, NSDictionary, NSSet, etc) without reverting to regular C?

    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

  • Help with pointers in Cocoa

    - by G.P. Burdell
    I'm trying to make a simple calculator application in cocoa. The program hangs when I click on one of my buttons. I think I've traced the problem to the part of my controller that adds a digit to the end of the number currently on the display: - (void)updateNumber:(int)buttonClicked{ *self.activeNumberPointer = *self.activeNumberPointer * 10 + buttonClicked; [outputField setFloatValue:*self.activeNumberPointer]; } I used a pointer to the "activeNumber" in order to allow my program to tell which of the two operands I'm editing. Any help appreciated, thanks.

    Read the article

  • In C: sending func pointers, calling the func with it, playing with EIP, jum_buf and longjmp

    - by Yonatan
    Hello Internet ! I need to make sure i understand some basic stuff first: 1. how do i pass function A as a parameter to function B? 2. how do i call function A from inside B ? now for the big whammy: I'm trying to do something along the lines of this: jmp_buf buf; buf.__jmpbuf[JB_PC] = functionA; longjmp(buf,10); meaning that i want to use longjmp in order to go to a function. how should i do it ? thank you very much internet people ! Yonatan

    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

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