0

I know that when passing strings between functions

char *str is almost the same as char str[n] in function header.

However i do wanna know why this would work and whats the difference between these 2 when working with string

I found that if i declare a "char *in" first then assign a string to it "in=string;" this wont work

can some one tell me whats behind this?

Is there anyway I can assign a array to another array? like in Java

Thanks

char[] one;
char[] two={'a','b'};
one=two;

3 Answers 3

1
char[] one;

The syntax is wrong. This isn't Java.

char* one;
char two[]={'a','b'};
one = two;

This works because array decays to a pointer.

"char *str is almost the same as char str[n] in function header."

NO. Arrays and pointers are two different concepts. They both aren't the same.

Sign up to request clarification or add additional context in comments.

1 Comment

As a function parameter declaration, char *str and char[n] are identical; they both declare str as a pointer. (This applies only to parameters.)
0

Array and Pointer are two different concepts. I guess you should start slowly with basics, this is one paper that explains : http://www.cs.bu.edu/teaching/cpp/string/array-vs-ptr/

4 Comments

the concepts of string and arrays are the same in cpp and c? thanks for the link anyway
In C++, you have std::string, which is a class for handling string. However, you can still use C-style string. In C, string is a concept not a type, an array of character ending with \0 (NULL) character. That's the difference!
@xjaphx: NULL is a null pointer constant, not a null character.
@Keith Thompson: ok, NUL then :|
0

The best explanation I've seen for the relationship between arrays and pointers in C (and C++) is section 6 of the comp.lang.c FAQ.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.