![]() |
|
#1
|
|||
|
|||
Using character pointers to store the person's nameHi, i am just started learning c. İ have a question, i know this is simple but know i can't do this.
Is there anyone who can help me ? i would be very thankful to him/her. My question is, a program that read the first name, middle name and last name of a person sperately and print it as the following format in a line: Chris Mark Brown How can i do this question With using POINTERS?? Thank you , iam waiting your answers... Last edited by JdS : 12-Mar-2004 at 07:29.
Reason: Please use a better title in your thread
|
|
#2
|
|||
|
|||
New to CHey,
The simplest solution I can think up is to use character pointers to store the persons names. For instance: CPP / C++ / C Code:
Then read the names from input and print them to the screen. If you have any code you have written so far it would be very much appreciated. Best of, Pan Thomakos |
|
#3
|
||||
|
||||
|
Sounds like a fine solution to me. Why are you learning pointers so early? Normally pointers aren't fully introduced to programming student until later in the course... my teacher didn't even get us going on pointers in the first course. He's using the 2nd course to take care of that stuff.
__________________
-Aaron |
|
#4
|
||||
|
||||
|
This is kind of a strange thing to do with "pointers".
If you use a definition like char* firstname you need to make sure that you allocate memory for it with a call like: CPP / C++ / C Code:
Actually, this is an antiquated way to use malloc. A much better way is CPP / C++ / C Code:
[/EDIT] This doesn't make much sense as it is just like a char array really. What exactly is it that you want to do with the pointers? I am assuming this is an assignment, but there has to be some object lesson to using the pointers. Is there any more information that you have? Last edited by dsmith : 12-Mar-2004 at 11:09.
Reason: Old habits die hard
|
|
#5
|
||||
|
||||
|
This is a very rudimentary task. I'll bet the instructor just wanted to get his students to use a char* instead of a string type.
__________________
-Aaron |
|
#6
|
|||
|
|||
|
Yes you are right this is an assignment.
I am confused so. (I Guess)i don't need to use pointers for this program. But we studied this last week ointers. So pointer's must be related to this program. The question was: *Write a program that read the first name, middle name and last name of a person sperately and print it as the following format in a line: Chris Mark Brown I tried this but doesn't work: [i initilize pointers but i can't use in the program] CPP / C++ / C Code:
Last edited by dsmith : 12-Mar-2004 at 14:10.
Reason: Please use [c] & [/c] for syntax highlighting
|
|
#7
|
|||
|
|||
|
I'll try a few hints.
Since each name is (probably) more than one character in length, each name requires more than a single char memory location. In C, there is no "string" data type, but "strings" are handled as arrays of char. So, maybe you can have something like this for starters: char first[40]; char second[40]; char sur[40]; Now, "40" is completely arbitrary, and is (probably) a reasonable guess for something like the longest name that we would expect. Now, declaring an array of char like this reserves 40 bytes of memory to hold each name. To get something into each array (so that the names can be printed out later), one way is to use scanf() with %s as the format specification. Now, you need to read up (class notes or text book) to find how "strings" are represented in char[] arrays, and how they are handled. Note to all: I know, I know, scanf() is not the way to go for reading user data from a console, but ---first things first --- let's get something compiled that can be executed. Good Luck: Hang in there. Dave |
|
#8
|
|||
|
|||
|
OK. i tried but what's my wrong now?
CPP / C++ / C Code:
Last edited by dsmith : 12-Mar-2004 at 14:10.
Reason: Please use [c] & [/c] for syntax highlighting.
|
|
#9
|
||||
|
||||
|
Hi internethesabi. That is a very long name.
![]() One thing to make your code easier for people to read, place it between [c] and [/c] when you post it. First of all, try to get out of the habit of using global variables. There are some instances when you do want globals, but unless you have a good reason I wouldn't use them. So put your variable declerations inside main as so: CPP / C++ / C Code:
Second, when you pass a char* or a char array to the scanf statement, there is no need to dereference it with the address symbol, as the variable is already pointing to a address so instead of: CPP / C++ / C Code:
You should just use: CPP / C++ / C Code:
Now, you can eliminate alot of work in this by just printing out the char array if you want. Once again this is simpler than you are making it, CPP / C++ / C Code:
However, if you do want to use your pointer assignment, once again, it can be much simpler than what you have, CPP / C++ / C Code:
The way you are doing it is pointing the first_name pointer at the 10th charecter in the array (which is pretty dangerous). Also, you may want to think about a bigger array than 10. Some names are longer than that, like internethesabi. ![]() |
|
#10
|
|||
|
|||
Thankx to all...I am very happy because just in a hour i solve my problem by this forum.
i understand completely and special thankx to dsmith i learn a lot of thing in your detailed post. Thanks a lot again... |
Recent GIDBlog
Halfway done! by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| problem with php5 cgi installation | fab13 | Apache Web Server Forum | 3 | 19-Nov-2003 09:11 |
| unwanted scrollbar problem | kelly001 | Web Design Forum | 3 | 24-Oct-2003 10:44 |
| link problem [1][2]----- | zuzupus | MySQL / PHP Forum | 0 | 16-Sep-2003 05:16 |
| A problem Between MySQL <> phpBB | mirable | MySQL / PHP Forum | 3 | 10-Sep-2003 05:31 |
| select problem | zuzupus | MySQL / PHP Forum | 0 | 15-Aug-2003 07:25 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The