in c/c++ you can declare a string:
char str[5]="hello"
and the compiler puts the chars right next to each in memory...
but if you try to print this string to the screen:
char *temp;
*temp=str[zero]; (zero = 0) mega tokyo format messes it up
print(temp);
it will print the first char 'h' and then junk. why does this happen because all the chars should print because they are right next to each other.
----------------------------------------------------------------
char *str="hello"
print(str);
this will print the string correctly. why?
arent the two the same? how would i go about print an array of chars that is declare like: char str[5]="hello"?
strings/ printing strings
Re:strings/ printing strings
A few things here the text hello is not 5 chars long its 6. {'H', 'e', 'l', 'l', 'o', '\0'} dont forget that null. But your problem atualy lies here
char *temp;
*temp=str[zero]; (zero = 0) mega tokyo format messes it up
print(temp);
char *temp;
*temp=&str[zero]; see the & (address of str zero)
print(temp);
You was appling the value of str[zero] to your pointer not the address besides you should simply be able to put print(str) because arrays and points are realy the same thing.
char *temp;
*temp=str[zero]; (zero = 0) mega tokyo format messes it up
print(temp);
char *temp;
*temp=&str[zero]; see the & (address of str zero)
print(temp);
You was appling the value of str[zero] to your pointer not the address besides you should simply be able to put print(str) because arrays and points are realy the same thing.
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:strings/ printing strings
Code: Select all
char *temp=str[0];
Code: Select all
char *temp=str+0;
Code: Select all
char *temp=&(temp[0]);
Code: Select all
&str[0]
btw, to avoid MT [ O ] bug, just put your code between [ code ] and [ / code ] tags (spaces are to be removed
Re:strings/ printing strings
You do need the & sigh for address of. Thats what its for...
This should aslo work
temp=str;
This should aslo work
temp=str;