Thread: Help with Pointer Arrays

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    14

    Help with Pointer Arrays

    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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    I've used the "%s" Format and every time i compile the program closes becuse of an error

    what i have is:
    Code:
    void print ( *array)
    
    {
        
    
    }
    i want to print that array that has all the string values
    one by one like if every word had a "\n"

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I've used the "%s" Format and every time i compile the program closes becuse of an error
    Post the smallest and simplest compilable program that demonstrates the error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    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

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    12
    Quote Originally Posted by slingerland3g View Post
    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?

    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;
    }
    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.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Isn't this code (from that FAQ) problematic in that "MyInt" is not defined as a global variable?
    It does not need to be a global variable, so it should not be 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.
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    12
    Quote Originally Posted by laserlight View Post
    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.
    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.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Does this differ in C (vs. C++)?
    No, not in this case.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    12
    Quote Originally Posted by laserlight View Post
    No, not in this case.
    Cool - thanks. I'll have to back and re-read my code with that in mind.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. pointer arrays!!!
    By condorx in forum C Programming
    Replies: 1
    Last Post: 05-14-2002, 08:55 AM
  4. pointer arrays
    By condorx in forum C Programming
    Replies: 3
    Last Post: 05-03-2002, 09:04 PM
  5. Replies: 4
    Last Post: 11-05-2001, 02:35 PM