Jec86N
March 24th, 2007, 12:13 PM
Can anybody tell me anything about pointers.
|
Click to See Complete Forum and Search --> : Pointers Jec86N March 24th, 2007, 12:13 PM Can anybody tell me anything about pointers. Jec86N March 24th, 2007, 12:30 PM Ok in my book I'm doing pointers and structures can someone please tell how that all works. awba March 24th, 2007, 12:49 PM It would be easier if you'd point out what you don't understand about it... Well, pointers only represent the physical memory adress of the data they are referring to. So whatever kind of data you have (strings or large structs) the corresponding pointer will always have four bytes. This can be extremely useful if you want to create a fast sort routine because you'd only sort pointers instead of huge amounts of data. Also you might want to pass variables to functions in order to process them - with a pointer you hand over the adress and that's it. On the other hand you have to be careful not to use pointers that point to the nirvana - an access violation will be the consequence. Jec86N March 24th, 2007, 02:47 PM ok here is my problem I have the code right but I have no input data is what its telling me. How can that be? answer March 24th, 2007, 02:51 PM Well then the code is indipendant from any input then. Pointers are declared as type *variable; where 'type' is the type and '*' indicates a pointer to 'type'. If the code is dependant on input, but you don't see its dependance, perhaps global variables are used ? John E March 24th, 2007, 03:05 PM Pointers are always one of the most difficult areas for new programmers to understand but there's really nothing difficult about them. Basically, a pointer is a special variable that gives the address of some other variable. Why would you want to know the address of a variable? Well, when you pass a variable as a parameter to a function you're calling, the compiler makes a copy of that variable (in other words, the called function isn't acting on the original variable - but on a copy of it). This leads to 2 problems:- 1) The copied variable obviously has a different address from the original and that address 'hides' the address of the original data. Only the address of the copy is known to the called function - so if you change some aspect of the data from within the called function, only the local copy gets changed - not the original variable. Often, you need the called function to act upon the original variable. 2) If the original variable is large (for example a big array of data) all that data gets copied onto the stack, which is very inefficient. Both of those problems are solved by using pointers. When you pass a pointer to a called function, only the pointer gets copied (which is just a few bytes). But because the pointer is pointing to the address of the original variable, the called function 'knows' this address and it can therefore make changes to the original data. A pointer is simply a way of letting a function know where a big chunk of data is - without needing to copy all the data. The most important thing you need to know about pointers is that a pointer must point to an area of RAM that's been specially prepared to hold some data. Here are some examples:- char* pMessage = "Hello"; // 'pMessage' points to 6 bytes of (pre-prepared) data char buffer[] = "Goodbye"; char* buf = buffer; // 'buffer' contains pre-prepared data - therefore 'buf' is a valid pointer char* ptr = new char[10]; // 'ptr' points to a pre-prepared buffer of 10 bytes (although the bytes uninitialised) char* ptr2; // 'ptr2' is pointing to completely random memory (very dangerous). C++ has a new type called a reference which offers most of the functionality of a pointer but which has similar syntax to a normal variable. References and pointers are closely related, though they're not identical. JVene March 24th, 2007, 04:19 PM Good one John E! The whole reason we have pointers to contend with in C++ and C is from the history of C. It was originally intended as an assembler like language, where statements would be interpreted in one or just a few assembler instructions. Since the underlying machine must deal with variables according to 'where' they're located in RAM, an artifact of assembler itself, pointers give that same level of access to RAM in C. Structures are loosely related to assembler, too, in that it's a way to 'lay out' a set of variables in a group, which was once done in assembler with memory offsets. The structure is a higher level of concept though. This snippet, for example: struct es { int a, b, c; }; es v; es * vp = GetAnEs(); int x = vp->b; int y = v.c; The structure is made of 3 values, all integers in this case. The compile lays this structure out for us, and we might assume that the location of b is 4 bytes from the type of structure, following a, but the compiler might have reason to 'pad' this space, optimizing for a 64bit compiler, meaning that we can't guarantee where b is located ourselves. We might assume, but would could assume incorrectly. Yet, it won't matter for most uses because for a statement like: int x = vp->b; The compiler 'knows' where it put b with respect to the location of the structure. It would know, therefore, that the structure located at the memory position identified by the pointer vp is the start of the structure, and if knows how 'far from the top of that structure' b is located. In this case the compiler copies the value identified by this location into x. int y = v.c; This is a little less involved with pointers. The location in memory is entirely handled by the compiler, so we can make this statement without any concern for where things are located. This was never true of assembler. In assembler everything is identified by its location, but in this statement the locations are entirely managed by the compiler. In: int x = vp->b; We're using a pointer, vp, which is a location in memory. A little more 'assembler like' use of the machine. We can know the offset to b with a 'member pointer' syntax thus: int es::*bptr = &es::b; This is asking for the offset of a 'b' in the 'es' structure. If we inquired with: printf("%d\n", (unsigned int) bptr ); ...a dubious practice, I'm sure....but... What we'd see is probably a 4, possibly an 8, printed. This is because the compiler may have choose to place b 4 bytes in from the type (following the integer a), or in a 64bit compiler, b might be 8 bytes in from the top. We don't 'normally' need this information, but it has it's uses in more advanced work, and it's helpful to understand the compiler is doing this under the hood. class and structures are related, the only difference being that classes are private by default, while structures are public by default. The convention may be that structures are typically used to represent a 'package' of data, like a customer record, whereas a class is typically used to represent a 'functional component' of some kind, with internal private data the external code should not be concerned with. This is only a hinted convention, and many of us choose to use class exclusively, making public some variables that seem appropriate for such access, and all else private or protected. The convention was chosen thus because structs existed in C, where private variables did not exist, and this left the struct otherwise unchanged. Jec86N March 24th, 2007, 08:47 PM Thanks you guys for your help. I think it work when I ran the program it work and it came up with 12 I don't know if thats right but it ran fine I guess. ovidiucucu March 25th, 2007, 06:37 AM [ Redirected thread ] codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |