Solved

Define pointers

Posted on 1997-12-07
10
186 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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.

777 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