We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Define pointers

luonght
luonght asked
on
Medium Priority
223 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)
Comment
Watch Question

ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
#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);

Commented:
Ozo, that should have been an answer.

Commented:
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);

ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
And since you don't want to leave c pointing to unallocated memory:

c = 0;

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

rbr

Commented:
Ozo, pls Answer the question.
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
/* 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 */

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
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.
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
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.
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
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);
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.