Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
hi, I need to creates string suffixes out of a Reference string. for eg. suffixes of abcdefg will be 1)bcdefg 2)cdefg 3)defg and so on... my idea was to create ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2011
    Posts
    6

    Exclamation Character Pointers


    hi,

    I need to creates string suffixes out of a Reference string.
    for eg. suffixes of abcdefg will be

    1)bcdefg
    2)cdefg
    3)defg
    and so on...

    my idea was to create an array of pointers to point to the first few characters and then use that pointer to print the rest of the string.
    But when i print using the pointer i get GARBAGE values!!!!

    shudn't std::cout<<ptr[w] print the string following the char it is pointing to? why do i get garbage values??

  2. #2
    Just Joined!
    Join Date
    Feb 2011
    Posts
    3
    Can you use library's?

    why not just use substr?
    cplusplus.com/reference/string/string/substr/

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,455
    Quote Originally Posted by nikhil. View Post
    why do i get garbage values??
    Please post your code here and we can tell you why. Usually it means that you are accessing bogus memory addresses.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  4. #4
    Just Joined!
    Join Date
    Feb 2011
    Posts
    6
    // creating 5 Suffixes
    int k=1;
    char *ptr[5]; //i need array of pointers
    while(k<6)
    {
    ptr[k]=refcpy+k; //refcpy is where my original string is stored
    k++;
    }

    //printing suffixes
    for(int w=1;w<=5;w++)

    {
    std::cout<<ptr[w]; // this step prints suffixes but with garbage values!!!
    std::cout<<"\n";

    }

  5. #5
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,455
    Ok. Your indexing of the array is bogus. C/C++ use 0-based indexing, so you should do this:
    Code:
    // creating 5 Suffixes
    // rubberman: note that it is better to use const char* type here
    // since you are not modifying them.
    const char* ptr[5]; //i need array of pointers
    for (int k = 0; k<5; k++)
    {
         ptr[k]=refcpy+k; //refcpy is where my original string is stored
    }
    
    //printing suffixes
    for(int w=0;w<5;w++)
    {
         std::cout<<ptr[w] << endl; // this step should print suffixes without garbage values.
    }
    P.S. I hope this isn't a school project - not allowed on the forums. If so, consider this a freebee.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  6. #6
    Just Joined!
    Join Date
    Feb 2011
    Posts
    6
    [QUOTE=P.S. I hope this isn't a school project - not allowed on the forums. If so, consider this a freebee.[/QUOTE]

    well, its a very small part of my project. Wanted to make sure i wasn't wrong conceptually!!

  7. #7
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,455
    Some computer languages use 1-based indexing for arrays. C and its derivatives (C++, Java, et al) use 0-based indexing. Something to remember. I think that Visual Basic uses 1-based indexing, though my memory may be faulty - I haven't done any VB programming in a LONG time.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  8. #8
    Just Joined!
    Join Date
    Feb 2011
    Posts
    6
    hehe i knw.. can't beleive that was my mistake!!!!

  9. #9
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,455
    Quote Originally Posted by nikhil. View Post
    hehe i knw.. can't beleive that was my mistake!!!!
    Live and learn! Good luck!
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  10. #10
    Just Joined!
    Join Date
    Feb 2011
    Posts
    6
    thank you!

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •