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 ...
- 02-25-2011 #1Just Joined!
- Join Date
- Feb 2011
- Posts
- 6
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??
- 02-26-2011 #2Just Joined!
- Join Date
- Feb 2011
- Posts
- 3
Can you use library's?
why not just use substr?
cplusplus.com/reference/string/string/substr/
- 02-27-2011 #3
- 02-28-2011 #4Just 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";
}
- 02-28-2011 #5Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Ok. Your indexing of the array is bogus. C/C++ use 0-based indexing, so you should do this:
P.S. I hope this isn't a school project - not allowed on the forums. If so, consider this a freebee.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. }Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 02-28-2011 #6Just 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!!
- 02-28-2011 #7Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
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!
- 02-28-2011 #8Just Joined!
- Join Date
- Feb 2011
- Posts
- 6
hehe i knw.. can't beleive that was my mistake!!!!
- 02-28-2011 #9
- 02-28-2011 #10Just Joined!
- Join Date
- Feb 2011
- Posts
- 6
thank you!


Reply With Quote
