Search Results

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

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

  • Why PHP Function Naming so Inconsistent?

    - by Shamim Hafiz
    I was going through some PHP functions and I could not help notice the following: <?php function foo(&$var) { } foo($a); // $a is "created" and assigned to null $b = array(); foo($b['b']); var_dump(array_key_exists('b', $b)); // bool(true) $c = new StdClass; foo($c->d); var_dump(property_exists($c, 'd')); // bool(true) ?> Notice the array_key_exists() and property_exists() function. In the first one, the property name(key for an array) is the first parameter while in the second one it is the second parameter. By intuition, one would expect them to have similar signature. This can lead to confusion and the development time may be wasted by making corrections of this type. Shouldn't PHP, or any language for that matter, consider making the signatures of related functions consistent?

    Read the article

  • C# memory management: unsafe keyword and pointers

    - by Alerty
    What are the consequences (positive/negative) of using the unsafe keyword in C# to use pointers? For example, what becomes of garbage collection, what are the performance gains/losses, what are the performance gains/losses compared to other languages manual memory management, what are the dangers, in which situation is it really justifiable to make use of this language feature... ?

    Read the article

  • Why can't I add pointers

    - by Knowing me knowing you
    Having very similiar code like so: LINT_rep::Iterator::difference_type LINT_rep::Iterator::operator+(const Iterator& right)const { return (this + &right);//IN THIS PLACE I'M GETTING AN ERROR } LINT_rep::Iterator::difference_type LINT_rep::Iterator::operator-(const Iterator& right)const {//substracts one iterator from another return (this - &right);//HERE EVERYTHING IS FINE } err msg: Error 1 error C2110: '+' : cannot add two pointers Why I'm getting an err in one place and not in both?

    Read the article

  • How are iterators and pointers related?

    - by sharptooth
    Code with iterators looks pretty much like code with pointers. Iterators are of some obscure type (like std::vector<int>::iterator for example). What I don't get is how iterators and pointer are related to each other - is an iterator a wrapper around a pointer with overloaded operations to advance to adjacent elements or is it something else?

    Read the article

  • STL class for reference-counted pointers?

    - by hasen j
    This should be trivial but I can't seem to find it (unless no such class exists!) What's the STL class (or set of classes) for smart pointers? UPDATE Thanks for the responses, I must say I'm surprised there's no standard implementation. I ended up using this one: http://www.gamedev.net/reference/articles/article1060.asp

    Read the article

  • Array of function pointers in Java

    - by Waltzy
    I have read this question and I'm still not sure whether it is possible to keep pointers to methods in an array in Java, if anyone knows if this is possible or not it would be a real help. I'm trying to find an elegant solution of keeping a list of Strings and associated functions without writing a mess of hundreds of 'if's. Cheers edit- 'functions' changed to 'methods', seems to bug people.

    Read the article

  • A few questions about char pointers.

    - by m4design
    1- How does this work: char *ptr = "hi"; Now the compiler will put this string in the memory (I'm guessing the stack), and create a pointer to it? Is this is how it works? 2- Also if it is created locally in a function, when the function returns will the memory occupied by the string be freed? 3- Last but not least, why is this not allowed: ptr[0] = 'H'; ?

    Read the article

  • At What point should you understand pointers?

    - by Vaccano
    I asked a question like this in an interview for a entry level programmer: var instance1 = new myObject{Value = "hello"} var instance2 = instance1; instance1.Value = "bye"; Console.WriteLine(instance1.Value); Console.WriteLine(instance2.Value); The applicant responded with "hello", "bye" as the output. Some of my co-workers said that pointers are not that important anymore or that this question is not a real judge of ability. Are they right?

    Read the article

  • Mixing C and C++, raw pointers and (boost) shared pointers

    - by oompahloompah
    I am working in C++ with some legacy C code. I have a data structure that (during initialisation), makes a copy of the structure pointed to a ptr passed to its initialisation pointer. Here is a simplification of what I am trying to do - hopefully, no important detail has been lost in the "simplification": /* C code */ typedef struct MyData { double * elems; unsigned int len; }; int NEW_mydata(MyData* data, unsigned int len) { // no error checking data->elems = (double *)calloc(len, sizeof(double)); return 0; } typedef struct Foo { MyData data data_; }; void InitFoo(Foo * foo, const MyData * the_data) { //alloc mem etc ... then assign the STRUCTURE foo.data_ = *thedata ; } C++ code ------------- typedef boost::shared_ptr<MyData> MyDataPtr; typedef std::map<std::string, MyDataPtr> Datamap; class FooWrapper { public: FooWrapper(const std::string& key) { MyDataPtr mdp = dmap[key]; InitFoo(&m_foo, const_cast<MyData*>((*mdp.get()))); } ~FooWrapper(); double get_element(unsigned int index ) const { return m_foo.elems[index]; } private: // non copyable, non-assignable FooWrapper(const FooWrapper&); FooWrapper& operator= (const FooWrapper&); Foo m_foo; }; int main(int argc, char *argv[]) { MyData data1, data2; Datamap dmap; NEW_mydata(&data1, 10); data1->elems[0] = static_cast<double>(22/7); NEW_mydata(&data2, 42); data2->elems[0] = static_cast<double>(13/21); boost::shared_ptr d1(&data1), d2(&data2); dmap["data1"] = d1; dmap["data2"] = d2; FooWrapper fw("data1"); //expect 22/7, get something else (random number?) double ret fw.get_element(0); } Essentially, what I want to know is this: Is there any reason why the data retrieved from the map is different from the one stored in the map?

    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

  • 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

  • Array of pointers in objective-c

    - by Justin
    I'm getting confused by pointers in objective-c. Basically I have a bunch of static data in my code. static int dataSet0[2][2] = {{0, 1}, {2, 3}}; static int dataSet1[2][2] = {{4, 5}, {6, 7}}; And I want to have an array to index it all. dataSets[0]; //Would give me dataSet0... What should the type of dataSets be, and how would I initialize it?

    Read the article

  • Trouble using opaque pointers in Objective C++

    - by morgancodes
    The answer to this quesion explains that opaque pointers are a good way to include C++ member variables in an Objective C++ header. I'm getting compile errors when trying to follow the example. Here's the relevant code from my header, with the corresponding compiler errors shown as comments: struct ADSR_opaque; // error: forward declaration of 'struct ADSR_opaque' @interface LoopyPulser : NSObject{ float _pulseRate; UInt32 tickInterval; UInt32 step; InMemoryAudioFile * audioFilePlayer; ADSR_opaque* env; // error: expected specifier-qualifier-list before 'ADSR_opaque' Pattern * pattern; float loopLengthRatio; float volume; } Is there something simple I'm doing wrong here?

    Read the article

  • Functions as pointers in Objective-C

    - by richman0829
    This is a question from Learn Objective-C on the Mac... Functions as pointers What I typed in, as per the recipe, was: NSString *boolString (BOOL yesNo) { if (yesNo) { return (@"YES"); } else { return (@"NO"); } } // boolString The pointer asterisk in the first line doesn't seem necessary, yet deleting it results in an error message. But what does it do? In NSString * boolString (yesNo); what seems to be going on is a function is defined as a pointer to an NSString. The function without the asterisk NSLog (@"are %d and %d different? %@", 5, 5, boolString(areTheyDifferent)); returns an NSString of YES or NO. But how can it return an NSString when it's a pointer? It might return the ADDRESS of an NSString; or if dereferenced it could return the CONTENTS of that address (an NSString such as YES or NO). Yet I see no place where it is dereferenced.

    Read the article

  • Dereferencing pointers without pointing them at a variable

    - by Miguel
    I'm having trouble understanding how some pointers work. I always thought that when you created a pointer variable (p), you couldn't deference and assign (*p = value) unless you either malloc'd space for it (p = malloc(x)), or set it to the address of another variable (p = &a) However in this code, the first assignment works consistently, while the last one causes a segfault: typedef struct { int value; } test_struct; int main(void) { //This works int* colin; *colin = 5; //This never works test_struct* carter; carter->value = 5; } Why does the first one work when colin isn't pointing at any spare memory? And why does the 2nd never work? I'm writing this in C, but people with C++ knowledge should be able to answer this as well.

    Read the article

  • Arrays & Pointers

    - by Thomas
    Hi, Looking for some help with arrays and pointers and explanation of what I am trying to do. I want to create a new array on the heap of type Foo* so that I may later assign objects that have been created else where to this array. I am having troubles understanding what I am creating exactly when I do something like the following. Foo *(*f) = new Foo*[10]; Also once I have created my array how do I access each element for example. (f + 9)->fooMember(); ?????? Thanks in advance.

    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

  • Initializing and accessing a pointer from an array of pointers

    - by idealistikz
    Suppose I have the following: void **Init(int numElems) { //What is the best way to intialize 'ptrElems' to store an array of void *'s? void **ptrElems = malloc(numElems * sizeof(void *)); return ptrElems; } //What is the best way to return a pointer pointing at the index passed as a parameter? void **GetPtr(void **ptrElems, int index) { void **elem = elems + (index * sizeof(void *)); return elem; } First, what is the best way to intialize 'ptrElems' to store an array of pointers? I use malloc because assigning it to an array will not persist after the end of the function. Second, what is the best way to point to the pointer at the specified index? I tried typecasting the first line of the 'GetPtr' function to ensure proper pointer arithmetic, but I receive the warning, 'initialization from incompatible pointer type'. Is it necessary to typecast?

    Read the article

  • Understanding c-pointers for rows in 2-dimensional array

    - by utdiscant
    I have the following code: int main() { int n = 3, m = 4, a[n][m], i, j, (* p)[m] = a; for (i = 0; i < n; i++) for (j = 0; j < m; j++) a[i][j] = 1; p++; (*p)[2] = 9; return 0; } I have a hard time understanding what p is here, and the consequences of the operations on p in the end. Can someone give me a brief explanation of what happens. I know c-pointers in their simple settings, but here it get slightly more complicated.

    Read the article

  • Javascript Pointers question with Dates

    - by Mega Matt
    I noticed this situation in my code (unfortunately), and was able to duplicate it in my own JS file. So I have this code: var date1 = new Date(); // today var date2 = date1; date2 = date2.setDate(date2.getDate() + 1); // what is date1? After this code executes, date1 is today's date + 1! This harkens back to my undergrad days when I learned about pointers, and I guess I'm a little rusty. Is that what's happening here? Obviously I've moved the assignment away from date1, and am only modifying date2, but date1 is being changed. Why is this the case? Incidentally, after this code executes date2 is a long number like 1272123603911. I assume this is the number of seconds in the date, but shouldn't date2 still be a Date object? setDate() should return a Date object... Thanks for the help.

    Read the article

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