pointers

C: local pointer variables change after sscanf problem

Hi I wrote a C code that a portion of it is: ... P *head=NULL,*cur=NULL; char Name,tmp[255]; int AT,ET; FILE *iF; if((iF=fopen(fileName,"r"))>0){ fgets(tmp,255,iF); sscanf(tmp,"Interval:%d\n",&quantum); fgets(tmp,255,iF); //waste while(!feof(iF) && fgets(tmp,255,iF)){ ...

Passing multiarrays into functions through pointer

Hello, how can I pass multiarray into function through pointer with c++. I can do this with simple arrays void Foo(int *arr) { } int someArr[10]; Foo(someArr); What about 2-dimensions array? ...

How to intialize an integer pointer in C++ ?

Hi, I have a very simple question. I want to write the below line of code in 2 lines : IplImage *image = 0; like : IplImage *image; image = 0; I want to know what I have written is correct or else I want to know how to write the correct one (in two lines). Thanks ...

Tiny crashing program

The following program compiles with g++ but then crashes upon running: class someClass { public: int const mem; someClass(int arg):mem(arg){} }; int main() { int arg = 0; someClass ob(arg); float* sample; *sample = (float)1; return 0; } The following program does not crash: int main() { float* samp...

Getting a subroutine to return three seperate arrays of random numbers in C

I currently have code for a subroutine to return a pointer to an array. This array is a list of random numbers for a one dimensional monte-carlo integral. I am now trying to do a multi dimensional equivalent which requires 3 arrays of random numbers and instead of having a separate subroutine for each I'm trying to make one which retur...

Pointers and Indexes in Intel 8086 Assembly

I have a pointer to an array, DI. Is it possible to go to the value pointed to by both DI and another pointer? e.g: mov bl,1 mov bh,10 inc [di+bl] inc [di+bh] And, on a related note, is there a single line opcode to swap the values of two registers? (In my case, BX and BP?) ...

Alternate way of computing size of a type using pointer arithmetic

Is the following code 100% portable? int a=10; size_t size_of_int = (char *)(&a+1)-(char*)(&a); // No problem here? std::cout<<size_of_int;// or printf("%zu",size_of_int); P.S: The question is only for learning purpose. So please don't give answers like Use sizeof() etc ...

Where in the C++ Standard does it say ::delete can change lvalues?

I ran into my first compiler that changes the lvalue passed to ::delete, but doesn't zero out the lvalue. That is the following is true: Foo * p = new Foo(); Foo * q = p; assert(p != 0); assert(p == q); ::delete p; assert(p != q); assert(p != 0); Note that p is not zero after the delete operation, and it has changed from it's o...

Swap arrays by using pointers in C++

I have two arrays of pointers to doubles that I need to swap. Rather than just copy the data within the arrays, it would be more efficient just to swap the pointers to the arrays. I was always under the impression that array names were essentially just pointers, but the following code receives a compiler error: double left[] = {1,2,3}; ...

Segmentation fault in equating a char pointer value to some char

Possible Duplicate: Why is this C code causing a segmentation fault? char* string = "abcd"; now when i try to change some character of this string i get segmentation fault *string = 'p'; or string[0] = 'p'; string[0] = 52; Can someone please explain me the reason that why is it happening. Thanks Alok.Kr. ...

pointer array and sizeof confusion

hey folks, why does this: char** pointer = new char*[1]; std::cout << sizeof(pointer) << "\n"; output 4? I have an array of pointers, but it should have length 1, shouldn't it? ...

returning reference of local variable

Hello, gcc 4.4.4 c89 In main I call a function to pass a line of text to a function. I want to perform some operation on it. However, that would mean that line is of no use. So in my get_string function I copy the contents and return the result. The only problem, is that the memory to that result would be lost and pointing to something...

dealloc or releasing best practice in objective-c?

I just simply release objects as such: [myObj release]; I have seen others add: [myObj release]; myObj = nil; Is the latter more advantageous because now there will never be a reference to that pointer? ...

what will be behavior of following code snippet ?

What will be the behavior and output of the following code if i accidentally code so in C/C++, float a = 12.5; printf("%d\n", a); printf("%d\n", *(int *)&a); ...

Why does the following program give a error?

Why does the following program give a warning? Note: Its obvious that sending a normal pointer to a function requiring const pointer does not give any warning. #include <stdio.h> void sam(const char **p) { } int main(int argc, char **argv) { sam(argv); return 0; } I get the following error, In function `int main(int, char **...

How to pass a 2D dynamically allocated array to a function?

Hello, I have a 2 dimensional array dynamically allocated in my C code, in my function main. I need to pass this 2D array to a function. Since the columns and rows of the array are run time variables, i know one way to pass it is : -Pass the rows,column variables and the pointer to that [0][0] element of the array myfunc(&arr[0][0],r...

Unable to call a pointer to function returning a pointer

Hi there, I'm stuck with a weird problem. I have two files a.c and b.c as follows: b.c: #include <stdlib.h> int *foo() { int *x; x = (int *) malloc(sizeof(int)); *x = 4; return x; } I compile b.c to b.so using gcc: $ gcc -o b.so -shared -fpic a.c: #include <stdio.h> #include <dlfcn.h> int main() { void *hdl; hdl = dlop...

Do classes with uninitialized pointers have undefined behavior?

class someClass { public: int* ptr2Int; }; Is this a valid class (yes it compiles)? Provided one assigns a value to ptr2Int before dereferencing it, is the class guaranteed to work as one would expect? ...

Iterator member behavior

I was expecting the second assert in the following to pass. I'm asking for your help. Edit: It didn't work when I had poss everywhere instead of poss_a in some places. #include <vector> #include <cassert> class Sampler { public: std::vector<int*> poss; std::vector<int*>::const_iterator poss_it; Sampler(std::vector<int*> poss_a) : ...

Using malloc() for unique pointers

So, I have some function returning a pointer, or NULL on error. Now I'd like to add another possible value for a different error/condition. I heard this mentioned in some other answer before - using malloc() to create a unique pointer that will serve as a possible value for such functions to return (so now the can return a proper pointer...