Pointers

This is a discussion on Pointers within the C++ Programming forums, part of the General Programming Boards category; hi all.. i'm a bit confused how the pointer works.. Code: std::string n; //is this right?? // i'll create an ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    30

    Arrow Pointers

    hi all..
    i'm a bit confused how the pointer works..

    Code:
    std::string n;
    
    //is this right??
    
    // i'll create an empty array of string with x elements in it
    char * str = new char [x]
    
    // if i want to fill in
    strcpy(str,n.c_str)
    let say n= "HELLO"
    after i copy it does this mean *str= "HELLO" or str = "HELLO" ????
    this also mean str[0] = 'H' ???


    and another thing on double pointer

    Code:
    //create new space
    
    char** word = new char* [num of str];

    so what is the best way for me to copy *str into **word so the ouput for **word = "HELLO" ?? what i mean here is the word[0] = "HELLO" ...


    thanks in advance

  2. #2
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Code:
    char str[] = "HELLO";
    
    char* pStr = str;
    
    printf("%s\n" , pStr);
    output would be HELLO
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    30
    i understand that..but in order to create stack that is not possible.i cant use STL or vector for this stack assignment..so im trying to understand how pointers works then...

    the way i think is:
    the first pointer will be a list of char
    the double pointer will be the list of word....


    the first pointer should give me in 'H' 'E' 'L' 'L' 'O'
    then in the second pointer will be "HELLO"... in the way the first pointer is a pointer to a list of stack...

    so what is the best way for me to copy value in the first pointer to the double pointer..??
    hm..does this makes sense to you? 0_0

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,293
    // i'll create an empty array of string with x elements in it
    Incorrect. The code below that comment creates an "uninitialised" array.
    "empty" would imply that it contains elements, which is not the case.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,293
    Sorry, I mean contains no elements.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    30
    hey iMalc.
    do you mean i need to initialise the new array i created then i can use it later on?..but how?
    most of the examples i've seen didnt initialise the new array.

    another que: do you know a good way of getting the string from *str to **word?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

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