Need some help with arrays and pointers

This is a discussion on Need some help with arrays and pointers within the C++ Programming forums, part of the General Programming Boards category; Can anyone help me with this code? My desired output is: String 1 = String1Text *PointerStr1 = s + t ...

  1. #1
    NIM
    NIM is offline
    Registered User
    Join Date
    Oct 2003
    Posts
    24

    Need some help with arrays and pointers

    Can anyone help me with this code?

    My desired output is:

    String 1 = String1Text
    *PointerStr1 = s + t + r + i + n + g + 1 + t + e + x + t

    I appreciate any and all of your help! Thankyou in advance!

    Here is my code:

    Code:
    #include <iostream> 
    
    using namespace std; 
    
    int main() 
    { 
    
    char string1[] = "Hello World"; 
    char *ptr; 
    char ptr; 
    
    cout << "The character string that you inititalized is : " << string1 << endl; 
    
    
    
    for (int i = 0; *ptr + 1 !=('\0'); ++ i) 
    
    if ((*ptr +1) == '\0') 
    cout << *ptr; 
    else 
    { cout << *ptr 
        << '+'; 
    } 
    
    cout << "The  string that is output : " << *ptr << endl; 
    
    
    return 0; 
    
    }

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    first of all you can't have two variables with the same name. first you declare char * ptr, and then char ptr;

    second, what is the for loop for? make the pointer point to the address of the first element of the array; it is as simple as that.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this.
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() 
    { 
    
    char string1[] = "Hello World"; 
    char *ptr;
    
    cout << "The character string that you inititalized is : " << string1 << endl;
    
    for (ptr = string1; *ptr !='\0'; ++ptr)
    
    if (*(ptr + 1) == '\0')
    cout << *ptr; 
    else 
    { cout << *ptr 
        << '+'; 
    } 
    
    return 0; 
    
    }
    Last edited by swoopy; 12-12-2003 at 12:26 PM.

  4. #4
    NIM
    NIM is offline
    Registered User
    Join Date
    Oct 2003
    Posts
    24
    I am supposed to initialize a character string to a text phrase and then write a function to traverse the contents of the string, using pointer-offset notation and then print each character seperated by the '+' sign.

    I started by having just one character initialization to a pointer. And I also had the pointer equal the string but continuously received errors. And then I added the char ptr; to see if that would offset some errors. No avail.

    Swoopy - I will definately try that out - Thanks!

  5. #5
    NIM
    NIM is offline
    Registered User
    Join Date
    Oct 2003
    Posts
    24
    you guys are AWESOME- perfect output and the desired results.

    this site rocks

  6. #6
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    I see, but I think you are missunderstanding the concept of a pointer. A pointer is not a variable; you cannot store a value in a pointer.

    so what you had in your original post: *PointerStr1 = s + t + r + i + n + g + 1 + t + e + x + t , can't be done. You set a pointer to point to an address of something else allready in the memory.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  7. #7
    NIM
    NIM is offline
    Registered User
    Join Date
    Oct 2003
    Posts
    24
    Axon - I think I am beginning to understand what a pointer does. I originally tried to declare string1=*ptr; but later revamped the code because I thought I coded that wrong in the declaration statement.

    I should have made the original post clearer to what exactly I was looking for.

    When I said "desired output" I wanted that desired output on the screen after the program was compiled. So, I should have better written my original problem better.

    I am sorry for the confusion.

    And thankyou again for all your help.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Or here's a second way.
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() 
    { 
    
    char string1[] = "Hello World"; 
    char *ptr;
    
    cout << "The character string that you inititalized is : " << string1 << endl;
    
    for (ptr=string1; *(ptr+1) !='\0'; ++ptr)
    {
       cout << *ptr << '+'; 
    } 
    cout << *ptr;
    
    return 0; 
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  2. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 03:49 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 04:31 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 01:41 AM

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