Simple Pointer Problem

This is a discussion on Simple Pointer Problem within the C++ Programming forums, part of the General Programming Boards category; Hi i am trying to get a pointer to output text if it is possible but i dont understand where ...

  1. #1
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215

    Simple Pointer Problem

    Hi

    i am trying to get a pointer to output text if it is possible but i dont understand where i am going wrong

    this is the code i have come up with so far

    Code:
      
    #include <iostream>
    using namespace std;
     
    int main()
    {
      char name[10];
      char *ptr;
    
      ptr = &name[40];
      cin>>name;
      cin.ignore();
      cout<< *ptr <<"\n";
      cin.get();
    
    }
    when i do run the program i get some weird symbol. is this because it is just pointing to the memory address location?

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,446
    For starters, name[40] is accessing the array beyond its boundary.
    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
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    well i have modified it a bit and i can get one letter from the charactures i have entered but it does not display the rest of them is

    here is the update.

    Code:
      char name[10];
      char *ptr;
    
      ptr = name[10];
      cin>>name;
      cin.ignore();
      cout<< *ptr <<"\n";
      cin.get();
    also another question is that i have 3 pointers in a program it there any way of changing this

    Code:
    struct example {
    int x;
    int y;
    int z;
    };
    
    int main()
    {
    example structure;
    example number;
    example final;
    example *ptr;
    example *ptr2;
    example *ptr3;
    
    structure.x = 1;
    number.y = 123;
    final.z = 1772748757;
    ptr = &structure;
    ptr2 = &number;
    ptr3 = &final;
    
    cout<< ptr->x <<"\n\n";
    cout<< ptr2->y <<"\n\n";
    cout<< ptr3->z;
    cin.get();
    is there away to make ptr 1, 2, 3 one pointer or do they have to be all seperate?

    Thanks

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,446
    I suggest that you read the tutorials on arrays and pointers.
    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
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
      ptr = name[10];
    This part is definitely and completely broken - it sets the pointer to the value of a char [that is one beyond the size of your actual array] [[and your array has not been initialized, so the behavious is completely undefined, not that you'd want to initiialize it beyond it's size anyways]]. If you want your pointer to point at name, use:
    Code:
    ptr = name;
    // or
    ptr = &name[0];
    // or - if you don't want the FIRST char:
    ptr = &name[5];
    It's not clear to me what you want to change in your second example. If you want to print the content from three different structures, you need to set a pointer three times - of course, you can reuse the same pointer multiple times, but that would mean changing the pointer between each output. A single pointer can only point at one location at any given time, but you can of course change the pointer to point at anything else whenever you like.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A problem with pointer initialization
    By zyklon in forum C Programming
    Replies: 5
    Last Post: 01-17-2009, 11:42 AM
  2. Problem with function's pointer!
    By Tirania in forum C Programming
    Replies: 5
    Last Post: 11-28-2008, 03:50 AM
  3. printf output makes no sense. Is it a Pointer problem?
    By officedog in forum C Programming
    Replies: 3
    Last Post: 10-03-2008, 09:01 AM
  4. Simple Pointer Problem
    By Atlas24 in forum C++ Programming
    Replies: 7
    Last Post: 11-29-2005, 12:34 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM

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