Solved

Define pointers

Posted on 1997-12-07
10
185 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
 
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
[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

 
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

Network it in WD Red

There's an industry-leading WD Red drive for every compatible NAS system to help fulfill your data storage needs. With drives up to 8TB, WD Red offers a wide array of solutions for customers looking to build the biggest, best-performing NAS storage solution.  

Question has a verified solution.

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

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures 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.

911 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

22 Experts available now in Live!

Get 1:1 Help Now