-
memory location
i was reading a tutorial in c and i found this...
Code:
main()
{
/* copy "hello" into str1. If str1 isn't big enough, hard luck */
strcpy(str1,"hello");
/* if you looked at memory location str1 you'd see these byte
values: 'h','e','l','l','o','\0'
*/
what do he mean by... looking at the memory location..... how would i do that...? how can i find out if the values are really hellow \0?
-
You can print out individual characters.
Code:
int main()
{
int i;
for ( i = 0; i < strlen(str1); i++ )
printf( "str1[%d] is %c", i, str1[i] );
}
-
As an extension to Nutshells, you can hex dump the array:
Code:
#include <stdio.h>
#include <ctype.h>
#define ARRAY_SIZE 20
int main(void)
{
char my_string[ARRAY_SIZE] = "hello";
int i;
for (i = 0; i < ARRAY_SIZE; i++)
{
printf ("%02x(%c) ", my_string[i], (isprint(my_string[i])?my_string[i]: ' '));
if ((i+1) % 10 == 0)
putchar ('\n');
}
return 0;
}
/*
Output:
68(h) 65(e) 6c(l) 6c(l) 6f(o) 00( ) 00( ) 00( ) 00( ) 00( )
00( ) 00( ) 00( ) 00( ) 00( ) 00( ) 00( ) 00( ) 00( ) 00( )
*/
-
if you want the memory location, you can do the following:
Code:
printf( "%p", &str );
if you want to see the values in the array and you have a robust IDE, you can run the debugger and see the values in the array.