I'm trying to Print each string on an array pointer
(one by one)
EXAMPLE:
JOHN
JAME
BILL
DOUG
SAM
So on.......
Which loop would help me do that with a pointer array
I'm trying to Print each string on an array pointer
(one by one)
EXAMPLE:
JOHN
JAME
BILL
DOUG
SAM
So on.......
Which loop would help me do that with a pointer array
You have an array of strings and want to print each string on a line? You can use printf("%s\n", str[i]) in a loop, where str is the array of strings and i the index of the current string.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I've used the "%s" Format and every time i compile the program closes becuse of an error
what i have is:
i want to print that array that has all the string valuesCode:void print ( *array) { }
one by one like if every word had a "\n"
Post the smallest and simplest compilable program that demonstrates the error.I've used the "%s" Format and every time i compile the program closes becuse of an error
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Using arrays and pointers can be tricky. The question is how are you passing your array to the function?
As Elysia has mentioned earlier, this may help:
Read up on Arrays and Pointers:
http://cpwiki.sourceforge.net/A_pointer_on_pointers
I have a question on this that maybe you can settle once and for all. Isn't this code (from that FAQ) problematic in that "MyInt" is not defined as a global variable?
Is it acceptable to pass a pointer to a value that is defined within the confines of a single method? I would think that would be a problem.Code:void foo(int* MyInt) { std::cout << "Address of MyInt: " << &MyInt << std::endl; std::cout << "Value of MyInt (address of original MyInt): " << MyInt << std::endl; std::cout << "Value of original MyInt: " << *MyInt << std::endl; } int main() { int MyInt = 100; std::cout << "Value of MyInt: " << MyInt << ", address of MyInt: " << &MyInt << std::endl; foo(&MyInt); return 0; }
It does not need to be a global variable, so it should not be a global variable.Isn't this code (from that FAQ) problematic in that "MyInt" is not defined as a global variable?
It is not a problem since the object pointed to continues to exist as it has not yet gone out of scope. It just happens that control is now in an inner scope, that of the function being called.Is it acceptable to pass a pointer to a value that is defined within the confines of a single method? I would think that would be a problem.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Does this differ in C (vs. C++)? I could swear that I've had problems in C where I've passed a pointer, to a variable not defined as a global variable, to another method and it's caused unpredictable results.
Could be coincidence, I suppose. I'm still very much learning.
If you pass a pointer to a variable into a function, that memory location pointed to will remain valid during the function call. C or C++, no difference.
Edit: I suppose it's possible that you passed a pointer to a string literal into a function, and you tried to overwrite that string literal. String literals are constant and can't be changed.
No, not in this case.Does this differ in C (vs. C++)?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)