Help with Pointer Arrays

This is a discussion on Help with Pointer Arrays within the C Programming forums, part of the General Programming Boards category; I'm trying to Print each string on an array pointer (one by one) EXAMPLE: JOHN JAME BILL DOUG SAM So ...

  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
    22,267
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,267
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,267
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,185
    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
    22,267
    Does this differ in C (vs. C++)?
    No, not in this case.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21