Tuesday, July 14, 2009

Pointers in C language?

What are two ways i could assign the address of an array element [3][2] to a pointer?

Pointers in C language?
like this





int *p;


p = %26amp;array[3][2];
Reply:int *ptr;


ptr = %26amp;array[3][2];





and





int *ptr1,*ptr2;


ptr1 = %26amp;array[3][2];


ptr2 = ptr1;
Reply:int* p;


p = %26amp;a[3][2];


int* q;


q = a[3] + 2;





In the first one, the "%26amp;" just means "the address of".





In the second one, because it's a 2-dimensional array a[3] just gives me the address of the beginning of the 4th row (row 1 would be a[0]), which I just add 2 to in order to advance the address 2 places further down the row.

surveys

What is wild pointer , Null Pointer,Void pointer & Dangling Pointer? How can we distinguish between them?

Related To Pointer in c

What is wild pointer , Null Pointer,Void pointer %26amp; Dangling Pointer? How can we distinguish between them?
In computer science, a pointer is a programming language datatype whose value refers directly to ("points to") another value stored elsewhere in the computer memory using its address. Obtaining the value that a pointer refers to is called dereferencing the pointer. A pointer is a simple implementation of the general reference datatype (although it is quite different from the facility referred to as a reference in C++).





Null Pointer


A null pointer has a reserved value, often but not necessarily the value zero, indicating that it refers to no object. Null pointers are used routinely, particularly in C and C++, to represent exceptional conditions such as the lack of a successor to the last element of a linked list, while maintaining a consistent structure for the list nodes. This use of null pointers can be compared to the use of null values in relational databases and to the "Nothing" value in the "Maybe" monad. In C, each pointer type has its own null value, and sometimes they have different representations.





Because it refers to nothing, an attempt to dereference a null pointer can cause a run-time error that usually terminates the program immediately (in the case of C, often with a segmentation fault, since the address literally corresponding to the null pointer will likely not be allocated to the running program). In Java, access to a null reference triggers a Java.lang.NullPointerException, which can be caught (but a common practice is to attempt to ensure such exceptions never occur). In safe languages a possibly null pointer can be replaced with a tagged union which enforces explicit handling of the exceptional case; in fact, a possibly-null pointer can be seen as a tagged union with a computed tag.





A null pointer should not be confused with an uninitialized pointer: a null pointer is guaranteed to compare unequal to a pointer to any object or function, whereas an uninitialized pointer might have any value. Two separate null pointers are guaranteed to compare equal.





Void Pointer


a void pointer (void *), which indicates that it is a pointer to a region of unknown data type. This pointer is typically cast to a more specific pointer type by the programmer before being used.


malloc returns a void pointer





Dangling Pointer


A reference that doesn't actually lead anywhere. In C and some other languages, a pointer that doesn't actually point at anything valid. Usually this happens because it formerly pointed to something that has moved or disappeared, e.g. a heap-allocated block which has been freed and reused.





Hope this will satisfy you.





R. Nag MS (Comp. Sc)


How pointers make a C program faster?

can use of pointers make a C program faster?

How pointers make a C program faster?
They allow direct memory manipulation.
Reply:Yes, using pointers will help you to control computer memory





For tutorials click here.





http://www.dreamincode.net/forums/showto...


What u mean by pointers(c-prgraming)?

Pointers literally mean what they say, they point to a memory location. Say for instance you have stored a variable x in a memory location. y is a pointer to the location of x. All it says is that I point to x and x is at this location and has a certain value. Pointer programming is very useful and is extensively used for several complicated problems. Pointers have a constant size of 4 bytes and can be used to point to variables of any type: char, int, long, etc.

What u mean by pointers(c-prgraming)?
They are special variables which hold memory address of another variable


a pointer is a separate variable that refers to another location in memory


C++ programing this is part of my study guide and im not sure if my answers are right could you help me out?

1.The arguments in a function call must match the arguments in


the function's header


a)only when the function call is in a different file from the function body.


b)in number, order, and variable name.


c)in number and order only.


d)in number, order, and type.


e)They don't have to match the header, only the function prototype.


2.To use I/O redirection to read from a file you need to:


a.declare a file variable in your program and openit


b.use the full path name with cin (a:\file1.dat.cin %26gt;%26gt;)


c.run your program from dos and add the file to be read from with the correct operator ( myprog %26lt; file1.dat)


d.any of the above will work


3.If you try to explicitly open an ifstream or ofstream file, how can you tell if the file opened successfully?


a.use the fail() function (method)


b.a return code of 1 indicates success


c.a pointer to a buffer that will receive data will be null if it fails


d.there is no way to tell other than to try to do input of output


e.a return code of 0 for success

C++ programing this is part of my study guide and im not sure if my answers are right could you help me out?
1-d


2-b


3-c





Please select best answer if all are correct

survey monkey

C++ and pointers? I wrote a few lines to help me understand this..please explain...easy question!?

#include %26lt;iostream%26gt;





int main(){








int* x;





int y = 4;





x = %26amp;y;





//is it correct to say something like x is a memory address and by putting %26amp;y, the compiler reads it as "take x, which is a variable that stores strictly a memory address and store the memory address which contains the content of the variable y"??





std::cout %26lt;%26lt; *x %26lt;%26lt; '\n';





int *z = x;





++*z;





//also for the above, int *z = x; is setting a variable z which strictly stores the address, to the address of x (which may or may contain data) and the ++*z is read by a compiler as (increment the value of the memory address z)





std::cout %26lt;%26lt; *z %26lt;%26lt; '\n';





return 0;


};





Once you declare a pointer such as int *ptr the rest of the block referring to ptr refers to the mem address and *ptr refers to the data at that address right?...where does %26amp;ptr fit into the picture?








Is what I am saying correct??? In your own words what does





int *ptr





ptr


%26amp;ptr


*ptr





all referring to?





Thanks!!

C++ and pointers? I wrote a few lines to help me understand this..please explain...easy question!?
See if this makes some sense to you.





If I say;





int y = 5;





I created the variable y (at this point you have no idea where in memory this variable is) and it put the 5 in that location. So refering to y in a program is now like saying get what is in y (the 5) and do something with it.


std::cout %26lt;%26lt; y %26lt;%26lt; \n;





Now say you happen to know that the memory location used for y is 1000.





If I said:





int *x = 1000; // Note the compiler is probably going to complain about this because 1000 isn't a proper integer pointer, but this is just to get the idea.


Is creating the variable x. And putting the memory location 1000 in it. Now I want to not what is in x, but what is at the location 1000, so I "dereference the pointer x". Which is given by *x, or in other words give me the value of the integer at that location that is stored in x. So in this case since x is pointing the the same location as y, *x will give me 5.





std::cout %26lt;%26lt; *x %26lt;%26lt; '\n";





Now for %26amp;y. This will give me that address (memory location) of where y is storing its value. So without knowing where y is storing its value I can do it like this:





int *x = %26amp;y;





That creates x and sets it to the memory location where y is storing its interger value. And is the proper way to set the pointer x because I'm setting the integer pointer x to the memory location where y is storing its value.





Note that even though the syntax says;


int *x;


*x





These are actually not the same. The *x is just the way to declare a pointer to an integer.





Where as *x is tell it to given you the integer value from where x is pointing to.
Reply:int *ptr /*this delcairs a pointer of type int. Which means that the ptr can point to any data type flag as an int type. eg int or int[ ]*/





ptr = the address of something.


%26amp;ptr = this is the address of the pointer ptr , in terms of memory storage


*ptr = this "dereferences" the pointer and returns the contence of the memory address contain in ptr.





for example.....





int* pointer; // at this point the pointer is swinging... if you deref it you will segfault this is because it contains a random address some where on your computer.





int number = 100;





// now we want to store the the memory address of "number" so we say. "%26amp;" mean "the address of"





pointer = %26amp;number





// now we want to out put the number that the point points to.


so we dereference the pointer... which mean that we look at the contence of the pointer and convert it to an address, then we move to the address location and return that data to the calling line.





cout %26lt;%26lt; *pointer %26lt;%26lt; endl;





// if we wish to alter the data begin referenced we can do this


// "*" means dereference. or n simple terms move to down the pointer to the thing being pointed at.





(*pointer)++;


//or


*pointer = *pointer + 1;








anyway i hope this helps


Again c++ and pointers ...?

I try to learn the pointers and in the same time to create a GTA-SA trainer but i'm having problems ...





i use this code :


"


int main ()


{








int *pointer;





pointer = reinterpret_cast%26lt;int*%26gt;(0xB7CE50);





*pointer = 100;








getchar();











}


"





But i get this:





"Unhandled exception at 0x004113c8 in memory.exe: 0xC0000005: Access violation writing location 0x00b7ce50."





I also tried to do pointer = 0xB7CE50; but won't compile,can not convert from int to int* .





So what now?





Can anyone give me a good and working example?


PS:You can use inline ASM!





Thanks!!!

Again c++ and pointers ...?
The reason this is throwing an exception is because the memory location you are trying to write to 0x00B7CE50 is protected by the operating system.





The proper way to get memory for a pointer is to utilize the c++ opperator 'new'





try this:





int* pointer;





pointer = new int();


// in straight C this would be pointer = (int*)malloc(sizeof(int));





*pointer = 100;