pointers

Should I use char** argv or char* argv[] in C?

I'm just learning C and was wondering which one of these I should use in my main method. Is there any difference? Edit: So which one is more common to use? ...

Pointer issue in C

Greetings, I am trying to learn pointers in C, I simply want my "addtwo" function to add 2 to every element of the input integer array, yet I get odd compilation errors, here is the non-pointer version which indeed won't properly compile. addtwo(int *arr[]) { int i=0; for(;i< sizeof(arr)/sizeof(int);i++) { arr[i] = arr[i...

Reversing a string in C

I have developed a reverse-string program. I am wondering if there is a better way to do this, and if my code has any potential problems. I am looking to practice some advanced features of C. char* reverse_string(char *str) { char temp; size_t len = strlen(str) - 1; size_t i; size_t k = len; for(i = 0; i < len; i++) { temp = str[k]; ...

What C compilers have pointer subtraction underflows?

So, as I learned from Michael Burr's comments to this answer, the C standard doesn't support integer subtraction from pointers past the first element in an array (which I suppose includes any allocated memory). From section 6.5.6 of the combined C99 + TC1 + TC2 (pdf): If both the pointer operand and the result point to elements of t...

Segmentation Fault with pointer-to-pointer with C / Linux.

In the following code I get a segmentation fault: Set *getpar() {...} char function(...) { Set **S; *S = getpar(); /* Segmentation Fault */ ... } But the bizarre thing is that with few changes there is no segmentation fault: Set *getpar() {...} ... char function(...) { Set *S; // One less '*' S = getpar(); // ...

How to control the mouse pointer outside our application

Hi, I want to control the mouse pointer with my application and be able to interact with other programs using my program, For example I want my application to be able to click on a button on another application How should I go about solving this problem? (Any programming language would work, also if you have any suggestion please let m...

Pointer to generic type

In the process of transforming a given efficient pointer-based hash map implementation into a generic hash map implementation, I stumbled across the following problem: I have a class representing a hash node (the hash map implementation uses a binary tree) THashNode <KEY_TYPE, VALUE_TYPE> = class public Key : KEY_TYPE; Value ...

Syntax for dereferencing a pointer in C (or C++)

I had a colleague check in code like this in C (syntax #1): (*(*(*p_member).p_member).p_member).member When I asked him why he didn't use -> (syntax #2): p_member->p_member->p_member->member he got really defensive stating that syntax #2 is more complicated than #1...I ended up changing his code because I had to modify it and cou...

Array of pointers to multidimensional arrays

Hi, i have some bidimensional arrays like: int shape1[3][5] = {1,0,0, 1,0,0, 1,0,0, 1,0,0, 1,0,0}; int shape2[3][5] = {0,0,0, 0,0,0, 0,1,1, 1,1,0, 0,1,0}; and so on. How can i make an array of pointers to those? I tried the following, but they don't work (warning: initializat...

Smart pointers/safe memory management for C?

Hi, I, and I think many others, have had great success using smart pointers to wrap up unsafe memory operations in C++, using things like RAII, et cetera. However, wrapping memory management is easier to implement when you have destructors, classes, operator overloading, et cetera. For someone writing in raw C99, where could you point ...

What is Joel's "two levels of abstraction"?

I was listening to the Stack Overflow Podcast #34 (59:00 - 1:02:00) and Joel mentioned the difficulty of pointers and recursion. He also mentioned thinking in two levels of abstraction. He also mentions these concepts in his Peril of JavaSchools article. .... But when you struggle with pointers, your program produces the line S...

C array address confusion

Say we have the following code: int main(){ int a[3]={1,2,3}; printf(" E: 0x%x\n", a); printf(" &E[2]: 0x%x\n", &a[2]); printf("&E[2]-E: 0x%x\n", &a[2] - a); return 1; } When compiled and run the results are follows: E: 0xbf8231f8 &E[2]: 0xbf823200 &E[2]-E: 0x2 I understand the result of &E[2] whic...

Is it right to say, that the value of a pointer is a memory address?

As I understand that (or think I understand), a variable that is a pointer stores just a memory address in it's value. lets say: int x = 5; NSString *str1 = [NSString stringWithCString:"one"]; then the value of x is 5. That's what I see in the Debugger when I put a breakpoint there. but: the value of str1 is NOT "one". It's a memo...

Variables, Pointers, Objects and Memory Addresses: Why do I get this strange result?

After I thought that I've understood how they work, I tried this: NSString *str1 = [NSString stringWithCString:"one"]; NSString *str2 = [NSString stringWithCString:"two"]; NSLog(@"str1: %x, %@", &str1, str1); //bfffd3cc, one NSLog(@"str2: %x, %@", &str2, str2); //bfffd3c8, two str1 = str2; NSLog(@"str1: %x, %@", &str1, str1); //bfffd3c...

Why does my instance variable behave so strange? Did I get it right?

For trying out some pointer stuff, I made an instance variable in an existing project. It's just declared like this: @interface PointerFun : NSObject { NSString* test; } I have no @property compiler directive, no @synthesize, so no getter and no setter. Just for testing. Then I have a method, where I try around some things with p...

Where are the best explanations of variables, pointers, references and memory addresses on the net?

Although I think that I've got that now (the light bulb is pretty bright now but still flickering a little bit), I'd like to read more stuff about pointers, variables, references, memory addresses, etc. Just the whole thing, i.e. what I have to understand when hearing thre term "reference" (think it's just a pointer, not sure). So let u...

What does object* foo(bar) do?

For some class C: C* a = new C(); C* b(a); //what does it do? C* b = a; //is there a difference? ...

Is it possible to create a vector of pointers?

Just wondering, because of a problem I am running into, is it possible to create a vector of pointers? And if so, how? Specifically concerning using iterators and .begin() with it, ie: How would I turn this vector into a vector of pointers: class c { void virtual func(); }; class sc:public c { void func(){cout<<"using func...

Reading arbitrary memory locations? Possible?

Is there a way to (read-only) access any arbitrary memory location without running into an access violation? I thought that each process has its own virtual adress space and that it can read all available memory locations...seems not to be the case, since my program hangs if I do something like var IntPtr : PInteger; AnInteger : Int...

C++ Problem initializing an object twice

Hi, I'm relatively new to C++ and am having a hard trouble understanding the instantiation of object and pointers to objects. Whats the difference between these two declaration in terms of memory and usage? : MyClass obj1; MyClass *obj2; And also the specific problem I am having is that I have a class which has an unsigned short arra...