0

greetings

just started with C/C++. Im not quite comfortable porting from java to c/c++. Pointers are quite hard at start. So pls help me with this problem

I want to copy the exact data of a certain pointer to an array of struct. Not just point a pointer from another. can this be done???

example(not a struct just to prove some point.)

char* a="dashdkasdhk";

char* b;
b=a;

this would say that "b" is pointing to the address of "a".
so how can i assign "b" with the values of "a" but "b" have its own memory alocation of its own. no just pointing to "a"

5
Contributors
6
Replies
12
Views
9 Years
Discussion Span
Last Post by Tuyen
0

If you're copying something, you need to allocate memory and then copy the appropriate values in. When dealing with strings, however, there's several library functions that'll help you along the way:

char* a = "hello world";
char* b;
b = strdup(a); // allocates and makes a copy of a
// ...
free(b);
0

would that work on structs? any alternative? lets say a more general way to solve it.

thanks

-1

i have a string of pointers
char *str[];

i want to pass str[1] into another function as argument.How to do it?
I have tried using
makefunc(&str)

//Definition: char makefunc(char *fun[])
--How to pass str?


Thanks

Votes + Comments
Don't bump old threads
0

Just use std::string and avoid all this char* nonsense.

Edited by Ariste: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.