![]() |
|
#1
|
||||
|
||||
Help with C++ pointersHey guys, who wants to do my homework for me? Just kidding. But Im working on another assignment for school that uses pointers. I have to create a string tokenizer class that accepts a string durring object creation and creates a pointer to the string. The class is also supposed to return the pointer to each token I think. You know, I don't even fully understand what is being asked of me. If it will help, you can read the assignment here:
debryro.uvsc.edu I don't even understand pointers that well. They are just memory addresses of variables and arrays right? Anyway, Im having a hard time getting started on this. Im getting these errors when trying to comile the class implementation (stringTokenizer.cpp): Quote:
Any insight would be great. Here is my code so far....... CPP / C++ / C Code:
In the meantime im going to read about pointers Last edited by Mjkramer21 : 15-Apr-2004 at 15:45.
Reason: used [c] tags instead of [c++]. Don't know if it makes a difference
|
|
#2
|
||||
|
||||
|
Hello MJ. One thing should let your program compile:
CPP / C++ / C Code:
An array is already basically a pointer, so assignment does not need an ampersand. When you give it an ampersand, you are saying the * to the *, thus your error. This also would work: CPP / C++ / C Code:
HTH |
|
#3
|
||||
|
||||
|
Can I actually place a delimiter into the array where I want using the pointer that I created in the class?
Something like this: CPP / C++ / C Code:
I tried it like this: CPP / C++ / C Code:
And guess what. Yea, didn't work. |
|
#4
|
|||
|
|||
|
jus change those double quotes to single quotes. that might work.
P.S. i think what your instructor wants you to do is that when you encounter a space, he wants you to put the rest of the string into a new string each time. the space itself is your delimiter. |
|
#5
|
||||
|
||||
|
Quote:
" " defines a string constant. You want to use ' '. (There is a space between each of those.) Is delimiter of type char? |
|
#6
|
||||
|
||||
|
Turning those quotes into single quote worked. How about that. I knew better
This assignment sucks! I have no idea how to do this. For one thing, I don't understand why I'm placing a space delimiter into an array that already has spaces as natural delimiters. I could understand if I was placing nulls into the places where there are spaces. I'm so close to getting this wrapped up. But I don't know what my next step is because I don't know what my instructor wants me to do exactly. The instructions say to pass a space delimiter, but thats gotta be wrong. With those instructions, as you can see in my code, im turning a ' ' into ' '. what is the point? Its gotta be a typo. He must want me to turn ' ' into '\0' or NULL. Is the whole point to find where there is a space in the input string, and then simply return the string all the way up until that space, at which time you would increment some temp pointer position in main and pass it again to read the next string until it finds a space? I hope that made sense. Here is my revised code if anyone wants to look over it and see if they can determine what Im trying to do or what my next step SHOULD be. CPP / C++ / C Code:
|
|
#7
|
|||
|
|||
|
yes that was precisely my point. your space itself is the delimiter, i think you need to place a null in place of a space and then assign a pointer to the next char element and repeat. that's the most efficient way.
the otherway is to assign new memory and use strcpy or lstrcpy functions. |
|
#8
|
||||
|
||||
|
Quote:
I just read through your assignment instructions. Your above sentence is basically correct. This is very similar to the strtok function if you are familiar with that. Do you want to modify the original string? (I would think not). If not, I would psuedo code this something like:
I don't know if that helps or not, but sometimes it helps to break it down a bit. Good luck! Last edited by dsmith : 16-Apr-2004 at 08:17.
|
|
#9
|
|||
|
|||
|
save which character to a temp char? and what do you mean by "lug" the track pointer with NULL?
|
|
#10
|
||||
|
||||
|
Here's what I see:
The constructor creates a char buffer and copies the parameter string into it. It creates two pointers and loads the beginning of the string into them. One pointer tokrtn will be the address of the token to be returned, the other the pointer to the next token toknxt . The delimiter is placed in the class delimiter character. The next method will
This will return NULL pointer when you've reached the end of the string. __________________
Cow: You're a lawyer too? Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase! |
Recent GIDBlog
2nd Week of IA Training by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| string conversion + pointers | Rolapin | CPP / C++ Forum | 0 | 02-Apr-2004 07:36 |
| Using character pointers to store the person's name | internethesabi | C Programming Language | 9 | 12-Mar-2004 15:44 |
| Passing Pointers To Pointers in Functions | elumira | C Programming Language | 8 | 05-Mar-2004 21:23 |
| c: array comparison | jack | C Programming Language | 7 | 26-Jan-2004 11:21 |
| pointers and arrays | jack | C Programming Language | 4 | 15-Jan-2004 12:27 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The