Thread: Pointers

  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,195
    Code:
    char str[] = "HELLO";
    
    char* pStr = str;
    
    printf("%s\n" , pStr);
    output would be HELLO

  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,318
    // 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,318
    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, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM