storing a value from a pointer to a character
Jun 16, 2011 at 9:12am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
unsigned char las;
unsigned char * pBuffer = "Hello" ;
for (unsigned int i=0;i<6;i++,las = pBuffer[i])
{
cout<<pBuffer[i]<<endl;
}
return 0;
}
I was trying to understand an already implememnted program.I saw this type of las = pBuffer[i] and was trying it out myself whether that is possible.But that program is running without any error.
I'm getting an error in the above code as "cannot convert from 'const char [6]' to 'unsigned char *'"
What do i do to make it run.
Thanks in Advance.
Jun 16, 2011 at 9:17am UTC
What is an
unsigned char
?
1 2 3 4 5 6 7
int main() {
char las;
const char *pBuffer = "Hello" ;
for ( unsigned int i = 0; i < 6; i++, las = pBuffer[i] )
cout << las << endl;
return 0;
}
Last edited on Jun 16, 2011 at 9:19am UTC
Jun 16, 2011 at 9:29am UTC
unsigned char will hold positive values as far my knowledge(0-255).
sorry i made a mistake in my code. The value i'm storing in pBuffer is "1234".
Jun 16, 2011 at 9:30am UTC
The program will won't print the H in Hello. The compiler should warn you about using las before it's been intialised.
Jun 16, 2011 at 9:37am UTC
Thanks MathHead200.
It runs when done as you told me to do.
Topic archived. No new replies allowed.