Thread: memory location

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    227

    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?
    Only the strong survives.

  2. #2
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    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] );
    
    }

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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( )
    
    */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    7
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  2. Memory allocation/reallocation
    By magda3227 in forum C Programming
    Replies: 10
    Last Post: 07-04-2008, 03:27 PM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  4. Location of member functions in memory
    By bennyandthejets in forum C++ Programming
    Replies: 4
    Last Post: 12-05-2003, 09:30 AM
  5. Managing shared memory lookups
    By clancyPC in forum Linux Programming
    Replies: 0
    Last Post: 10-08-2003, 04:44 AM