pointers

Passing pointers/references to structs into functions

This is going to sound like a silly question, but I'm still learning C, so please bear with me. :) I'm working on chapter 6 of K&R (structs), and thus far through the book have seen great success. I decided to work with structs pretty heavily, and therefore did a lot of work early in the chapter with the point and rect examples. One of ...

New approach for adding a new Node to a Linked List

void addNewNode (struct node *head, int n) { struct node* temp = (struct node*) malloc(sizeof(struct node)); temp -> data = n; temp -> link = head; head = temp; } The code give above is the popularly wrong version of a function for adding a new node at the head of a linked list. Generally the correct versions are like, ...

C error with pointer and const char[]

I have a const char arr[] parameter that I am trying to iterate over, char *ptr; for (ptr= arr; *ptr!= '\0'; ptr++) /* some code*/ I get an error: assignment discards qualifiers from pointer target type Are const char [] handled differently than non-const? ...

How to use find algorithm with a vector of pointers to objects in c++?

I want to find in a vector of Object pointers for a matching object. Here's a sample code to illustrate my problem: class A { public: A(string a):_a(a) {} bool operator==(const A& p) { return p._a == _a; } private: string _a; }; vector<A*> va; va.push_back(new A("one")); va.push_back(new A("two")); va.push_b...

Function parameters: Copy or pointer?

I'm kind of new to C++ and have some questions, this is one of them. Is there ANY reason when you are using a function that takes in one or several parameters, parameters of which you know will always be stored in a variable before the function call, to pass a copy of the variable, rather than a pointer to the variable? I'm talking in...

what is `int *arr[3][4]` pointing to?

I am modifying some code and came across a declaration that I am having trouble understanding: int *arr[3][4] = {0}; What exactly is this pointing to? Is it a matrix where every element is a pointer? Or is it pointing to a matrix of size [3][4]? Thanks ...

In C++ You Can Have a Pointer to a Function, Can you also have a pointer to a class?

I'm not talking about a pointer to an instance, I want a pointer to a class itself. ...

C++: how to cast 2 bytes in an array to an unsigned short

I have been working on a legacy c++ application and am definitely outside of my comfort-zone (a good thing), was wondering if anyone out there would be so kind as to give me a few pointers (pun intended) I need to cast 2 bytes in an unsigned char array to an unsigned short. The bytes are consecutive. For an example of what I am trying...

Using far function pointers

I realize that far is compiler specific, but my expectation is that the placement of the far specifier should make sense to those who really understand pointers. So, I have two applications that share the processor's entire memory space. App A needs to call function foo that exists in app B. I know the memory location of function foo....

C++ strings: [] vs. *

Hi all.. Been thinking, what's the difference between declaring a variable with [] or * ? The way I see it: char *str = new char[100]; char str2[] = "Hi world!"; .. should be the main difference, though Im unsure if you can do something like char *str = "Hi all"; .. since the pointer should the reference to a static member, which ...

Should I use static_cast or reinterpret_cast when casting a void* to whatever

Both static_cast and reinterpret_cast seem to work fine for casting void* to another pointer type. Is there a good reason to favor one over the other? ...

Incrementing C pointers

I thought that if a c pointer pointing to a char array was incremented then it would point to the next element in that array. But when I tried this I found that I had to increment it twice. Trying the increment using sizeof(char) I found that adding the size of a char was too much so it had to be divided by two. #include <stdio.h> int...

Pointer Pointer Methods C++

Hi, I have two questions: 1) How can I make an array which points to objects of integers? int* myName[5]; // is this correct? 2) If I want to return a pointer to an array, which points to objects (like (1)) how can I do this in a method? ie) I want to impliment the method: int **getStuff() { // what goes here? return *(myName); // ...

Have I completed this C++ pointers / lists assignment? [StackOverflow Code Review? :)]

A friend of mine is studying Engineering in college. I've never studied / used C++ as I've always stuck to .NET and web based programming. When I heard her Intro. to Programming course was going to teach them the basics of C++ I decided to get her to send me notes / assignments each week so I would have some definite material to work fro...

How can I reserve memory for pointer to an array in Delphi?

Hi everybody. I'm developing class to represent special kind of matrix: type DifRecord = record Field: String; Number: Byte; Value: smallint; end; type TData = array of array of MainModule.DataRecord; type TDifference = array of DifRecord; type TFogelMatrix = class private M: Byte; N: Byte; Data: ^...

What is forward reference in C?

What is forward reference in C with respect to pointers? Can I get an example? ...

C++: delete vs. free and performance

Consider: char *p=NULL; free(p) // or delete p; What will happen if i use free and delete on p? If a program taking more time for execution, suppose 10 minutes, is there any way to reduce its running time to 5 minutes? ...

Is Pointer Variable also Assigned a Memory Address?

In C++: On stack, a simple variable is assigned a memory address so that we can use pointer to contain this memory to point to it; then is a pointer also assigned a memory address? if yes, we can have pointer of pointers now? Thanks! ...

What am I doing wrong with this pointer cast?

I'm building a GUI class for C++ and dealing a lot with pointers. An example call: mainGui.activeWindow->activeWidget->init(); My problem here is that I want to cast the activeWidget pointer to another type. activeWidget is of type GUI_BASE. Derived from BASE I have other classes, such as GUI_BUTTON and GUI_TEXTBOX. I want to cast...

When should static_cast, dynamic_cast and reinterpret_cast be used?

I am reasonably proficient in C++, but I do not have a lot of experience using the cast operators to convert pointers of one type to another. I am familiar with the risks and benefits of pointer casting, as well as the evils of using C-style casts. What I am looking for is a primer on the proper ways to use the various cast operators in ...