pointers

What is the proper way to return an object from a C++ function ?

I am confused between : returning an object (but then the object is copied from the local variable in the function, which consumes memory) returning a pointer (but then you have to remember to delete it, in the calling code, which is weird) returning a reference (but this is not possible because this would be a reference to a local var...

Copying value from char pointer to a char array

I am having a pointer *ip_address_server which holds the ip address of the server : in_addr * address = (in_addr * )record->h_addr; char *ip_address_server = inet_ntoa(* address); Clearly, when I use printf to print the value of it, it gets nicely printed. printf("p address %s" , ip_address_server); But now if I declare...

Ensuring a reference that will not become invalid is given

I'm making a TextField class, which at the moment stores a reference to a variable, and whenever the programmer wants, they can call TextField.show() and the referenced variable will be read and displayed on-screen. This however can result in problems if the reference becomes invalid. For example, if the variable referenced goes out of ...

Arrays are Pointers??

Possible Duplicate: Is array name a pointer in C? Are arrays and pointers implemented differently? I have come across this question because , in both the cases we access elements from the starting address of an element.So , there should be close relation between them . Please explain the exact relation between them . Thanks. ...

Knowing the number of asterisks to use in method definitions in C?

For example: void DeleteLastNode(NodeType **L) If **L represents the address of the variable L, can it not be replaced by &L instead? Also, if I were to use *L or simply just L, would I not be able to change L's contents? If so, what's the difference of using *L and L, if L is a pointer to it anyways? Would I not be able to look at it...

c++ pointer management with local variables

I've got a method that creates some Foo and adds it to a vector of Foos. Foos are in charge of deleting their Bars during destruction. The Foo constructor takes a pointer of Bars and a size of them. When the function returns, the local Foo gets deleted and destroys its Bars, however I get a valid Foo object back. How should I be handlin...

Prevent last element in char * vector from changing

I am reading strings in C++ using fread where I am reading and storing shortSiteText in siteNames_. siteNames_ is declared as std::vector<char*> siteNames_; I use siteNames_ in other functions but because shortSiteText is a pointer, when I call the delete command on it, the last entry in siteNames_ is changed. How do I prevent this? fo...

Filling 4 variable in a struct type and using malloc

I need to write a simple program that ask the user to insert 4 double type variable in a struct variable data. struct Data { double a; double b; double c; double average; }; struct Data *ptr_name; int i; First, ask user the size: printf("Please enter the size:"); scanf("%d", &size); Then, use ...

C++ program crashes

I've this assignment to implement strcmp function. Sometimes it runs okay but other times it crashes. Please help me. #include <iostream> using namespace std; int mystrcmp(const char *s1, const char *s2); int main() { cout<<mystrcmp("A","A")<<endl; cout<<mystrcmp("B","A")<<endl; cout<<mystrcmp("A","B")<<endl; cout...

How do I set a pointer to Null at run-time (C++)

Right now I have a pointer set to a row in my 2D array. I want that pointer to stop pointing to that row, but I will be using the pointer later for something else. I just want to know how to unset the pointer after it is initialized and has pointed to a row. double* tempRow; tempRow = (double*) malloc(size * sizeof(double)); ... te...

Initialization of a pointer array

Hi all, I'm facing a problem initializing an array with pointers to members of a structure. The structure members have to be accessed through a structure pointer. The reason for this is we initialize the pointer at runtime to a memory mapped address location. The following code snippet is an example of the problem; #include <stdio.h> ...

array of objects,c++

i have two classes namely Flight and Runway. Now i am trying to pass an array of these objects as parameter to a function. void fun(Flight ptr1[],Runway ptr2[]) { ... ... } ptr1 should point to an array of Flight objects and ptr2 should point to an array of Runway objects. Now inside this function fun() how do i access members of the...

Not sure why I have "EXC Bad Access" error.

Hello everyone! I've programmed for a while in Java and .Net, but never really used C or Objective C. I'm still trying to understand a few concepts. I was working on a simple program just to see how I can make an array of structures. Which I believe I got right. I'm having a hard time figuring out how to access the subclasses and store ...

Comparing pointers for self-assignment

Hi, I am trying to overload the = operator on a simple C++ class called Set that contains a dynamic array of ints. For the = operator, I first want to check for self assignment, so I wanted to compare 2 pointers to make see if they have the same memory address. Here's the code: Set& Set::operator=(const Set& setEqual) { //first check fo...

What is the datatype of pointer in c?

are pointers of integer or unsigned datatype? ...

Pointers to objects in function scope

When you create some pointers inside the scope of a function, what happens when the function goes out of scope? Do they get destroyed or should I call delete on them at some point? void XMLDocument::AddNode(XMLNode& node) { std::string val = node.GetNodeName(); TiXmlElement* el = new TiXmlElement(val.c_str()); // What about th...

definition pointer variable

hi everybody, i can't understand following defining pointer variable. can you help me? double(*)(double *) foo; note : sory, i edit name of variable func to foo. ...

pointer to std::vector of arbitrary type (or any other templated class)

Hi! let's say i want to have a member variable for a pointer to std::vector but i do not want to specify what type of variable it stores. I want to access only those functions that are independant of it's actual generic type. is this possible with c++? something like this: class Foo{ public: void setVec(std::vector* someVec){ ...

Pointing a pointer to another pointer - what happens ?

Hi all, Wonder if anyone can clear up the following for me. What happens if I point a pointer to another pointer ? E.g Pointer 1 = Object Pointer 2 = Pointer 1 What if I then change pointer 1 : Pointer 1 = Object 2 Where does pointer 2 now point ? Thanks, Martin ...

C structure pointer dereferencing speed...

Hello. I have a question regarding the speed of pointer dereferencing. I have a structure like so: typedef struct _TD_RECT TD_RECT; struct _TD_RECT { double left; double top; double right; double bottom; }; My question is, which of these would be faster and why? CASE 1: TD_RECT *pRect; ... for(i = 0; i < m; i++) { if(p[...