pointers

what does compiler do with a[i] which a is array? And what if a is a pointer?

I was told by c-faq that compiler do different things to deal with a[i] while a is an array or a pointer. Here's an example from c-faq: char a[] = "hello"; char *p = "world"; Given the declarations above, when the compiler sees the expression a[3], it emits code to start at the location ``a'', move three past it, and fetch the ch...

passing pointers from C to C++ and vice versa

Is there any tips one can give me about passing pointers to structs, doubles, functions, ... from a C program to a C++ library and back? ...

auto_ptr released without assigning its return value

So what happens to a pointer if you release an object owned by auto_ptr but do not actually assign it to a raw pointer? It seems like it's supposed to be deleted but it never gets the chance to. So does it get leaked out "into the wild"? void usingPointer(int* p); std::auto_ptr<int> point(new int); *point = 3; usingPointer(point.rele...

Odd Memory Error -- Bad Alloc

Working on a WinPCap project. Trying to do some basic pointer and memory operations and having lots of errors. I've included the two lines I'm trying to run along with the includes. The same lines in another VSC++ project work just fine. This is the error I am getting Unhandled exception at 0x75a79617 in pktdump_ex.exe: Microsoft ...

In a C++ template function can I return a dereferenced argument type ?

What I mean is the following. I want a template function that takes two vector iterators (or two pointers to array of double) and returns a double that is somehow related to the vector iterators or array pointers that I pass. However, I want this to work for double or int, or any arithmetic type. I think I'm not allowed to say: templat...

accessing Double pointer

typedef struct _WDF_USB_DEVICE_SELECT_CONFIG_PARAMS { ULONG Size; WdfUsbTargetDeviceSelectConfigType Type; union { struct { PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor; PUSB_INTERFACE_DESCRIPTOR* InterfaceDescriptors; ULONG NumInterfaceDescriptors; } Descriptor; struct { PURB Urb; } Urb; struct { UCHAR Numb...

Accessing a structure variable double pointer

Some code: typedef struct _WDF_USB_DEVICE_SELECT_CONFIG_PARAMS { ULONG Size; WdfUsbTargetDeviceSelectConfigType Type; union { struct { PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor; PUSB_INTERFACE_DESCRIPTOR* InterfaceDescriptors; ULONG NumInterfaceDescriptors; } Descriptor; struct { PUR...

C++ member layout

Hello! Let's we have a simple structure (POD). struct xyz { float x, y, z; }; May I assume that following code is OK? May I assume there is no any gaps? What the standard says? Is it true for PODs? Is it true for classes? xyz v; float* p = &v.x; p[0] = 1.0f; p[1] = 2.0f; // Is it ok? p[2] = 3.0f; // Is it ok? ...

Incrementing variables using pointers

I'm very new to dealing with pointers, and my C knowledge is fairly small. I'm trying to understand pointers. I wrote the following code to print a list of variables (a to f) like so: 0 1 2 3 4 5 I wrote the following code to do this: #include <stdio.h> int main(){ int a,b,c,d,e,f; int *p; int i; a = b = c = d = f = 0; p = ...

What datatype to use for image data to avoid std:bad_alloc?

I'm developping an imaging library and I'm struggling with the image data datatype Since images can have variable datatypes (8 bits per pixel, 16 bits per pixel) I thought of implementing my image data pointer to void* pimage_data; however void* leads to all kind of nastiness including ugly pointer arithmetics such as pimage_data ...

C: swapping pointers in an array

I have an array that contains pointers. How can I swap two pointers - say array[1] and array[4] - correctly? ...

Pointers in C: when to use the ampersand and the asterisk

I'm just starting out with pointers, and I'm slightly confused. I know & means the address of a variable and that * can be used in front of a pointer variable to get the value of the object that is pointed to by the pointer. But things work differently when you're working with arrays, strings or when you're calling functions with a point...

Multiple Pointer Test in If Statment

considering a pointer to a struct struct a_struct { int A; }; Is it ok to do : struct a_struct *ptr; //... if( ptr != NULL && ptr->A == 1) { //work with ptr struct } or should you Test if the pointer is valid before testing for on of its field. if(ptr != NULL) { if(ptr->A == 1) { ...

Do these statements about pointers have the same effect?

Does this... char* myString = "hello"; ... have the same effect as this? char actualString[] = "hello"; char* myString = actualString; ...

What was the most useful time you used a pointer-to-pointer and/or reference-to-pointer in a c/c++ project?

Pointers-to-pointers and references-to-pointers seem overly complicated at first, especially for newbies. What is the best and most useful reason you used one in a c/c++ project you worked on? This can help people better understand pointers and how to use them effectively. Edited: Included C as well as C++ ...

Pointers and Functions in C++

From the lectures notes of a course at university, on "call-by-value": void fun(int *ip) { *ip =100; } called by int n=2; int *np; np = fun(np); would change the value of n to 100. When we say "int *ip", what exactly do we mean? A pointer of type integer? If so, when we call fun() with np as its argument, shouldn...

reference to the pointed object

Dereferencing pointers can make the code very hard to read. What I usually do is putting a reference to the pointed object and working with the reference. Example: shared_ptr<std::vector<int> > sp = get_sp_to_vector(); std::vector<int>& vec = *sp; ... vec.push_back(5); I wonder if it's a good practice. Does it have any drawback? Upda...

Program stops without error on struct member assignment

I have a function which accepts a pointer to a struct and sets a member of that struct to a specific value. However, after that assignment code is executed, and my program exits without showing any errors. void swn_addClassToInstance(struct instanceR *instance) { instance->classCount = 0; //nothing below here will run } I'm new to C...

Whats the best way to send QStrings in a function call?

I would like to know what is the most efficient and practical way of sending a Qstring as a parameter to a function, in QT more specifically. I want to use a reference. The problem is I also want to instantiate that string in the function itself like so for example: this is the function prototype: void myFunction(QString & theMsg); th...

C issue - Can't figure how to assign pointer to beginning of list

I have a simple assignment that the professor wants us to do. Basically to pull in some numbers from a text file and load into a linked list. I don't want to get to much into the details but I have a basic question. He provided us with a function like so: INTLIST* init_intlist( int n ) { INTLIST *lst; lst = (INTLIST *)malloc(sizeof(IN...