LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-25-2011, 01:38 AM   #1
nikhil.stephen
LQ Newbie
 
Registered: Feb 2011
Posts: 2

Rep: Reputation: 0
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??
 
Old 02-25-2011, 02:00 AM   #2
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by nikhil.stephen
shudn't std::cout<<ptr[w] print the string following the char it is pointing to?
Could you explain this with some code? What is 'ptr'? How have you stored the string "abcdef"?

yy.cpp
Code:
#include <iostream>
using namespace std;

int main ()
{
     char* dd = (char*)"abcdef";
     
     cout << *dd << "\n";
     cout << dd << "\n";
     
     cout << *(dd+1) << "\n";
     cout << dd+1 << "\n";

     return 0;
}
Output:
Code:
anisha@linux-uitj:~> g++ -Wall -Wextra yy.cpp
anisha@linux-uitj:~> ./a.out
a
abcdef
b
bcdef
 
Old 02-28-2011, 02:33 AM   #3
nikhil.stephen
LQ Newbie
 
Registered: Feb 2011
Posts: 2

Original Poster
Rep: Reputation: 0
// 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";

}

Last edited by nikhil.stephen; 02-28-2011 at 02:35 AM.
 
Old 02-28-2011, 03:09 AM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,226

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Code:
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
	const char *ptr = "abcdefg";

	for (int i = 0; i < strlen(ptr); i++)
	{
		cout << &ptr[i] << endl;
	}

	return 0;
}

Last edited by dugan; 02-28-2011 at 03:14 AM.
 
  


Reply

Tags
c++



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash scripting: parsing a text file character-by-character Completely Clueless Programming 13 08-12-2009 09:07 AM
To know the function on checking whether a character is ascii or unicode character. murugesan Programming 2 01-23-2009 01:07 PM
Problem using cout to print character pointers gregorian Programming 6 05-06-2008 04:28 PM
SIGSEGV: Array of character pointers?? usercsr Programming 2 04-23-2005 11:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration