Solved

Define pointers

Posted on 1997-12-07
10
188 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
ID: 1256544
#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
ID: 1256545
Ozo, that should have been an answer.
0
 
LVL 1

Expert Comment

by:messiah
ID: 1256546
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

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

c = 0;

0
 
LVL 10

Expert Comment

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

0
 
LVL 10

Expert Comment

by:rbr
ID: 1256549
Ozo, pls Answer the question.
0
 
LVL 84

Accepted Solution

by:
ozo earned 100 total points
ID: 1256550
/* 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
ID: 1256551
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
ID: 1256552
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
ID: 1256553
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
why debugging a macro i s difficult 10 41
Grammars for C C++ and java 1 138
What is sub-make ? 2 87
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 74
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

740 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