Thread: question about basic programming

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    11

    question about basic programming

    there's something that I dont understand
    how can you print a string using a pointer? what is the logic standing behind it?
    like for example in the following code:
    Code:
    #include <stdio.h>
    
    
    int main(void) {
        
        char *pointer="hello";
        printf("the string is:%s",pointer);
        
        
        return 0; 
    }
    why is it working?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,484
    That's the only way to print a string in one step.
    The %s format for printf expects a pointer to a char.

    Here are some examples.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      char a[] = "hello";
      char *b = "hello";
    
      // I assume you understand this
      printf("The string is %s\n", "hello" );
    
      // the array name 'a' decays to '&a[0]'
      printf("The string is %s\n", a );
      printf("The string is %s\n", b );
    
      // explicitly point at the first element
      printf("The string is %s\n", &a[0] );
      printf("The string is %s\n", &b[0] );
    
      // explicitly point at the 4th element
      printf("The string is %s\n", &a[3] );
      printf("The string is %s\n", &b[3] );
    
      // now for some weirdness
      // a "string" is just an anonymous char array.
      printf("The string is %s\n", &"hello"[0] );
      
      return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    137
    Quote Originally Posted by Maor Elbaz View Post
    how can you print a string using a pointer?
    Because the definition of a "string" in C is actually a character array where the pointer points to the first character in the array. The string lasts until it ends with a null terminator '\0'. I have a question on the board recently where I forgot to place a null terminator at the end of a character array and the program blew through the memory and put garbage values into my array, because it had no idea where to stop since I didn't provide a null terminator.

    So to recap one more time:

    1. "String" is just a character array.

    2. A Character array is represented by a pointer to the first value. So if you have char charArray[50] for example, charArray points to the memory location of the first element of the array.

    3. You can also use a char pointer directly instead of the bracket notation, which also points to the memory location of the first array element.

    4. C knows the end of the array by the '\0' character. Without that character, it does not know where the "string" ends.

    5. "Strings" as they exist in other programming languages, don't really exist in C, or at least are represented as character arrays.

    6. Please make a more appropriate thread title next time such as "How can you print a string using a pointer" or "Char pointer confusion." There is nothing in your thread title that explains what you are asking at all.
    Last edited by Asymptotic; 12-14-2016 at 04:40 AM.

  4. #4
    Registered User
    Join Date
    Dec 2016
    Posts
    11
    Okay, thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about basic programming
    By Maor Elbaz in forum C Programming
    Replies: 1
    Last Post: 12-12-2016, 05:10 AM
  2. question about basic programming
    By Maor Elbaz in forum C Programming
    Replies: 3
    Last Post: 12-10-2016, 04:43 PM
  3. Basic C Programming Question
    By goldeneye007 in forum C Programming
    Replies: 1
    Last Post: 12-06-2012, 06:04 PM
  4. basic bash programming question
    By Jimb0 in forum Linux Programming
    Replies: 4
    Last Post: 05-05-2011, 10:55 PM
  5. A basic math programming question
    By hebali in forum C Programming
    Replies: 38
    Last Post: 02-25-2008, 04:18 PM

Tags for this Thread