![]() |
|
#1
|
|||
|
|||
Another pointer problem!Hello everyone!
This program is supposed to reverse a string of characters using pointers alone. No new character array. It should work (coz its not my logic, but my teacher's) but I am not very good at pointer syntax at the moment. So could anyone please point out whats wrong. It is giving an "assertion error", I am not very clear what that is either (no compiling errors). CPP / C++ / C Code:
|
|||
|
#2
|
|||
|
|||
Re: Another pointer problem!OK. I found the problem which is you delete start and end but didn't request it from heap memory.
delete start; delete end; One new one delete. __________________
Linux is the best OS in the world. |
|
#3
|
|||
|
|||
Re: Another pointer problem!Thanks Peter!
You mean I didn't initialize? |
|
#4
|
|||
|
|||
Re: Another pointer problem!Quote:
No, he means that you should never apply the delete operator to something you didn't get from the new operator. Regards, Dave |
|
#5
|
|||
|
|||
Re: Another pointer problem!So I should remove the delete statements? How else do you deallocate them?
I just compiled it without using them and it works correctly! Thanks dave and Peter! My theoretical knowledge of pointers isn't good anyone, so could you tell me what wrong with this code (for output) CPP / C++ / C Code:
If you don't write append a star to str[i], then the program works perfectly. I thought * referred to the content of a pointer, so why doesn't it work? |
|
#6
|
|||
|
|||
Re: Another pointer problem!Quote:
Quote:
Quote:
str is the name of an array of chars. Element number i is accessed with the notation str[i]. Since str[i] is not a pointer, it is illegal to dereference it. See Footnote. So the loop might look like: CPP / C++ / C Code:
Now whether that does what you want it to do is something else, but it is, at least, legal C++ code. Regards, Dave Footnote: If p is a pointer data type and i is an integer data type, the following two notations are identical: CPP / C++ / C Code:
is the same as CPP / C++ / C Code:
Now, if p is the name of an array, then the compiler treats it as a constant pointer whose value is the address of the first element of the array, so the above two expressions are valid (and they are equivalent to each other) whether p has been declared to be a pointer to char or an array of char. Note that I am not saying that pointers are the same as arrays. They are not. It's just notation. |
|
#7
|
|||
|
|||
Re: Another pointer problem!Thanks Dave for the explanation!
But aren't they almost interchangeable? Pointers and arrays. I was having one more problem with the same code. When I edit it to be able to vary the size of the string, it doesn't work. Gives me that Microsoft Error: pointer.exe has encountered a problem etc. Why is that happening? Here is the edited code: CPP / C++ / C Code:
} |
|
#8
|
|||
|
|||
Re: Another pointer problem!Quote:
However... When you declare an array, you have a certain amount of storage. You can put stuff in that storage and you can get the same stuff back out, as long as nothing else in your program writes to that storage (accidentally or on purpose). A pointer is a variable. If you give it a value that points to some storage for which your program has legal access, you can dereference that variable to put stuff in that storage and get the same stuff back out, as long as nothing else in your program writes to that storage (accidentally or on purpose). You can set the value of a pointer variable to anything, but you can legally dereference it only if its value is the address of some storage for which your program has legal access. So you can assign the value of a pointer to legal memory by using the new operator to get a block of memory from the system. You can assign the value of a pointer by setting it equal to the address of a member of an array that you have declared (or the address of any other variable currently in scope). You can do arithmetic on that variable's value. So, if a pointer has a value that is the address of an element of an array, and if you increment the pointer value, its value is the address of the next element in the array (assuming you haven't incremented it to point beyond the end of the array). Once the pointer is set to some legal memory address, you can access that memory using pointer notation, such as *p = x; or you can use array notation, such as p[0] = x; Both notations mean exactly the same thing. (Not "almost," not "usually," not "nearly the same as," but exactly.) Quote:
The variable a is uninitialized. Its value is undefined, so the amount of memory that the new operator gives is unknown. Maybe it's big enough to hold what you want it to, or maybe not. Dereferencing str is undefined behavior. (Could cause a program crash; could appear to "work"; could do just about anything.) If I were going to implement the program similar to your example, my main() might look something like this: CPP / C++ / C Code:
Regards, Dave Footnote: When you use the name of an array as a function argument, what is actually passed to the function is a pointer whose value is the address of the first element of the array. The function itself has absolutely no way (no way) of knowing whether the thing refers to an array or some pointer whose value was obtained from new. If you call the function with the name of an array, the function has no way (no way) of knowing the size of the array unless you tell it (either with another integer data type argument or by using "global" variable or #define statement). There is no way to define a function that operates on an array so that the function can know the size of the array unless you tell it. The function always will be working on a pointer whose value was passed to it from the calling function. Yes, always. Your Reverse function could have been defined and declared as CPP / C++ / C Code:
There is absolutely no difference (really!) between this and the declaration CPP / C++ / C Code:
That's right, no difference! But that still doesn't mean that arrays are "almost interchangeable" with pointers. It's just notation. |
|
#9
|
|||
|
|||
Re: Another pointer problem!Thanks a lot Dave! You cleared so much up!
So a pointer itself points to the first element of the array, and when you increment it, it points to the next element and so on? With regards to the function not able to know the size, why is it then working correctly right now, without being given the size? |
|
#10
|
|||
|
|||
Re: Another pointer problem!Quote:
Quote:
A common use of arrays of chars is to hold so-called "C-style strings." All of the problems with knowing of the array and making sure it is large enough to handle your particular "string" are reduced, if not completely eliminated by using C++ standard library std::string objects. In my opinion, for the vast majority of C++ applications, std::string objects are more appropriate than the legacy "C-style strings" consisting of zero-byte terminated sequences of chars (in an array of chars). After you cover the std::string class, you may not want to use "C-style strings" in arrays of chars any more. I mean, you still should know about and understand "C-style strings," for a number of reasons, but, in my opinion, std::string objects are (usually) the way to go. You won't have to worry with new or delete. It's done automatically! Regards, Dave |
Recent GIDBlog
New Corolla Altis, 10th Generation - Part I by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Return pointer to template's nested class problem | Kimmo | CPP / C++ Forum | 2 | 20-Sep-2007 23:39 |
| Pointer Problem | Esam | CPP / C++ Forum | 1 | 22-May-2006 15:39 |
| [Tutorial] Pointers in C (Part II) | Stack Overflow | C Programming Language | 0 | 27-Apr-2005 17:36 |
| Please help! Newbie pointer problem! | robsmith | C Programming Language | 5 | 12-Mar-2005 04:48 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The