pointers

What's the Difference Between func(int &param) and func(int *param)?

In the following code, both amp_swap() and star_swap() seems to be doing the same thing. So why will someone prefer to use one over the other? Which one is the preferred notation and why? Or is it just a matter of taste? #include <iostream> using namespace std; void amp_swap(int &x, int &y) { int temp = x; x = y; y = temp;...

What will pNext be in the following case using C/C++?

func() { Object* pNext; func1(pNext); } func1(Object* pNext) { pNext = Segement->GetFirstPara(0); } I was expecting it to be pointer to firstpara returned from func1() but I'm seeing NULL can some explain and how to fix it to actually return the firstpara() pointer? ...

[C] How can I initialize an array of pointers to structs?

Is it possible to initialize an array of pointers to structs? Something like: struct country_t *countries[] = { {"United States of America", "America"}, {"England", "Europe"}, {"Ethiopia", "Africa"} } I want to do that in order to get the entities in not-contiguous memory, and the pointers to them in contiguous mem...

Printing pointers in C

I was trying to understand something with pointers, so I wrote this code: #include <stdio.h> int main(void) { char s[] = "asd"; char **p = &s; printf("The value of s is: %p\n", s); printf("The direction of s is: %p\n", &s); printf("The value of p is: %p\n", p); printf("The direction of p is: %p\n", &p); p...

Is there any way to determine the size of a C++ array programmatically? And if not, why?

This question was inspired by a similar question: How does delete[] “know” the size of the operand array? My question is a little different: Is there any way to determine the size of a C++ array programmatically? And if not, why? Every function I've seen that takes an array also requires an integer parameter to give it the size. But...

can realloc Array, then Why use pointers?

This was an job placement interview I faced. They asked whether we can realloc Array, I told yes. Then They asked - then why we need pointers as most of the people give reason that it wastes memory space. I could not able to give satisfactory answer. If any body can give any satisfactory answer, I'll be obliged. Please mention any situat...

How to read from a memory mapped I/O port in .Net?

Can standard pointers in .Net do this? Or does one need to resort to P/invoke? Note that I'm not talking about object references; I'm talking about actual C# pointers in unsafe code. ...

Pointer question

I am new to C and i have this question. why does the following code crash: int *a = 10; *a = 100; ...

Why does a pointer change itself during function transition?

In the following case I'm calling a Func with pointer passed to it, but in the called function, the parameter shows the pointer value as something totally bogus. Something like below. bool flag = Func(pfspara);--> pfspara = 0x0091d910 bool Func(PFSPARA pfspara) --> pfspara = 0x00000005 { return false; } Why does pfspara change t...

What is the arrow operator (->) synonym for in C++?

I know it, forgets it and relearn it again. Time to write it down. ...

What is the correct way to declare a pointer to a __far pointer?

On an embedded target I use far pointers to access some parts of the memory map. near pointer (without explicitely specifying __near): unsigned int *VariableOnePtr; Pointer to near pointer: unsigned int **VariableOnePtrPtr; far pointer: unsigned int *__far VariableTwoPtr; What is the correct way to declare a pointer to a far point...

So you think you know pointers?

I was shown this recently, and thought this was a really cool piece of code. Assume 32-bit architecture. #include <stdio.h> int main(void) { int x[4]; printf("%p\n", (void*) (x)); printf("%p\n", (void*) (x + 1)); printf("%p\n", (void*) ( printf("%p\n", (void*) ( } Without compiling the program, what does it output...

What is the difference between these declarations in C?

Hi friends, I am Manoj here to ask a question again. In C and C++ what do the following declarations do? const int * i; int * const i; const volatile int ip; const int *i; Are any of the above declarations wrong? If not what is the meaning and differences between them? What are the useful uses of above declarations (I mean in whic...

Malloc Error: incorrect checksum for freed object

I am working on implementing tail for an assignment. I have it working correctly however I seem to be getting an error from free at random times. I can't see, to track it down to a pattern or anything besides it is consistent. For example if I call my program as "tail -24 test.in" I would get the the the incorrect checksum error at the...

How can I get the size of an array from a pointer in C?

I've allocated an "array" of mystruct of size n like this: if (NULL == (p = calloc(sizeof(struct mystruct) * n,1))) { /* handle error */ } Later on, I only have access to p, and no longer have n. Is there a way to determine the length of the array given just the pointer p? I figure it must be possible, since free(p) does just that. ...

C: Pointers and Arrays Question

I keep reading that, in C, using pointer arithmetic is generally faster than subscripting for array access. Is this true even with modern (supposedly-optimizing) compilers? If so, is this still the case as I begin to move away from learning C into Objective-C and Cocoa on Macs? (As is the primary goal of my picking up C -- get enough th...

difference between pointers?

What is the difference between int *a[3] and int (*a)[3]? ...

Functions in C

Can we call functions using function pointer? if yes how? ...

how to use array of function pointers?

how to use array of function pointers in c? how to initialize them? ...

C++: dynamically allocating an array of objects?

This is kind of a beginners question, but I haven't done C++ in a long time, so here goes... I have a class that contains a dynamically allocated array, say class A { int* myArray; A() { myArray = 0; } A(int size) { myArray = new int[size]; } ~A() { // Note that as per MikeB's...