Solved

Define pointers

Posted on 1997-12-07
10
184 Views
Last Modified: 2010-04-02
How can I define three character pointers a, b, and c.
They are not arrays, and not initialized.
Make a point to "Hello", and b point to "World".
Concatenate these tow strings into c.
(Make sure c has enough memory allocated)
0
Comment
Question by:luonght
10 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
#include <string.h>
char *a,*b,*c;
a = "Hello";
b = "World";
c = (char *)malloc(strlen(a)+strlen(b)+1);
strcpy(c,a);
strcat(c,b);

0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
Ozo, that should have been an answer.
0
 
LVL 1

Expert Comment

by:messiah
Comment Utility
The answer was a comment eh? :)

BTW, it's good programming practice to free up allocated memory after you're done with it :)

ie. free(c);

0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
And since you don't want to leave c pointing to unallocated memory:

c = 0;

0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
Of course, in C, you don't need the (char*) cast - and it _can_ be dangerouse to leave it there.

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 10

Expert Comment

by:rbr
Comment Utility
Ozo, pls Answer the question.
0
 
LVL 84

Accepted Solution

by:
ozo earned 100 total points
Comment Utility
/* Answer for rbr: */
#ifdef _SVID
#include <malloc.h>
#else
#include <stdlib.h>
#endif
#include <string.h>
#include <stdio.h>
char *a,*b,*c;
a = "Hello";
b = "World";
c = malloc(strlen(a)+1+strlen(b)+1);
sprintf(c,"%s %s",a,b);
/* we could free(c) here, but I'd prefer to wait till after it's been used */
0
 
LVL 1

Expert Comment

by:_Zaphod
Comment Utility
Since the subject seems to be new to pointers, it might be prudent to add that you should _not_ try to modify *a or *b (including a[whatever] and b[whatever]) since that would be trying to modify parts of the executable in run-time, which will, in turn, cause a GPF or a segfault, depending on your operating system.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
It looks like luonght was less interested in geting this answered than rbr was.
luonght, if you don't like the answer, please reject it, if you do, please grade it.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
char *a,*b,*c;
a = strdup("Hello"); /* if you want *a to be modifiable */
b = strdup("World");
c = (char *)malloc(strlen(a)+strlen(b)+1);
strcpy(c,a);
strcat(c,b);
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now