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"

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

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

thanks

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

Comments
Don't bump old threads

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

Edited 6 Years Ago by Ariste: n/a

This question has already been answered. Start a new discussion instead.