GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Mar-2004, 14:52
internethesabi internethesabi is offline
New Member
 
Join Date: Mar 2004
Posts: 4
internethesabi is on a distinguished road
Arrow

Using character pointers to store the person's name


Hi, 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  
Old 11-Mar-2004, 15:11
pan pan is offline
New Member
 
Join Date: Mar 2004
Location: Greece
Posts: 16
pan will become famous soon enough

New to C


Hey,

The simplest solution I can think up is to use character pointers to store the persons names. For instance:

CPP / C++ / C Code:

char *first_name, *middle_name, *last_name;


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  
Old 11-Mar-2004, 19:59
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
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  
Old 11-Mar-2004, 21:46
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
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:
firstname = (char*) malloc(100);
[EDIT]
Actually, this is an antiquated way to use malloc. A much better way is
CPP / C++ / C Code:
firstname = malloc( 100 * sizeof( char ) );
which should work on all modern compilers. See here for an explanation.
[/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  
Old 11-Mar-2004, 23:33
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
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  
Old 12-Mar-2004, 12:07
internethesabi internethesabi is offline
New Member
 
Join Date: Mar 2004
Posts: 4
internethesabi is on a distinguished road
Angry

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 weekointers. 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:
#include <stdio.h>

char *first_name, *middle_name, *last_name;
char first, second, sur;

main()
{
	printf( "\nPlease write your Firstname:" );
	scanf( "%c", &first);
	printf( "\nPlease write your Secondname:" );
	scanf( "%c", &second);
	printf( "\nPlease write your Surname:" );
	scanf( "%c", &sur);
	printf( "\n");

	printf( "%c", first );
	printf( "%c", second );
	printf( "%c", sur );

	return 0;
}
Thanks for all
Last edited by dsmith : 12-Mar-2004 at 14:10. Reason: Please use [c] & [/c] for syntax highlighting
  #7  
Old 12-Mar-2004, 12:53
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,710
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
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  
Old 12-Mar-2004, 13:56
internethesabi internethesabi is offline
New Member
 
Join Date: Mar 2004
Posts: 4
internethesabi is on a distinguished road
Lightbulb

OK. i tried but what's my wrong now?

CPP / C++ / C Code:
#include <stdio.h>

char *first_name, *mid_name, *last_name;
char first[10], second[10], sur[10];

main()
{
	printf( "\nPlease write your Firstname:" );
	scanf( "%s", &first);
	printf( "\nPlease write your Secondname:" );
	scanf( "%s", &second);
	printf( "\nPlease write your Surname:" );
	scanf( "%s", &sur);

first_name = &first[10];
mid_name = &second[10];
last_name = &sur[10];

	printf( "%c ", *first_name );
	printf( "%c ", *mid_name );
	printf( "%c ", *last_name );

	return 0;
}
Last edited by dsmith : 12-Mar-2004 at 14:10. Reason: Please use [c] & [/c] for syntax highlighting.
  #9  
Old 12-Mar-2004, 14:22
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
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:
main()
{
  char *first_name, *mid_name, *last_name;
  char first[10], second[10], sur[10];

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:
scanf( "%s", &first);

You should just use:
CPP / C++ / C Code:
scanf("%s", first);

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:
printf( "%s ", first );
will print out the string just fine. Remember it is a string (%s) and not a char (%c).

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:
first_name = first;
Should do the trick.

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  
Old 12-Mar-2004, 15:44
internethesabi internethesabi is offline
New Member
 
Join Date: Mar 2004
Posts: 4
internethesabi is on a distinguished road
Talking

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 you are so kind.
i learn a lot of thing in your detailed post.

Thanks a lot again...
 
 

Recent GIDBlogHalfway done! by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 20:09.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.