Thread: pointers to char arrays.

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    1

    pointers to char arrays.

    hello, I am trying to understand how pointer works, but there is something I dont get:

    insert
    Code:
    #include <stdio.h>
    
    main()
    {
            int *pointer;
            int number=10;
            pointer=&number;
            printf("\nvalue of number: %d",number);
            printf("\nvalue of what the pointer points to: %d",*pointer);
            printf("\nmemory location of number: %x",&number);
            printf("\nmemory location of pointer: %x",pointer);
    }
    in this example I assing to pointer the memory location of number (pointer=&number), and everything works.

    I was trying to do the same with a string of characters, but it doesnt work as I expected:

    insert
    Code:
    #include <stdio.h>
    
    main()
    {
            char *pointer;
            char string[]="blabla";
            pointer=&string[0];
            printf("\ncontent of string: %s",string);
            printf("\ncontent of what the pointer points to: %s",*pointer);
    }
    the second program core dumps printing the value of *pointer.

    Dont understand why.

    I arrived to this example because I was thinking about a way to prevent the use of the malloc system call.

    I mean that I can define a string in this way:

    char *string=malloc(10 * sizeof(char));

    but i thought I could do the same defining a pointer to char, a char array and make the pointer points to the first character of the char array

    PS: I have found that a small change in the second program produces the expected behaviour:

    insert
    Code:
    #include <stdio.h>
    
    main()
    {
            char *pointer;
            char string[]="blabla";
            pointer=&string[0];
            printf("\ncontent of string: %s",string);
            printf("\ncontent of what the pointer points to: %s",pointer);
    }
    I dont understand why this works... I was expecting that *pointer would point to the char array

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    note that *pointer is referring to the first character in the string, not the entire string. When you are using %s in your print statements, the argument is expecting (char*) not (char)
    Code:
    int get_random_number(void)
    {
       return 4; //chosen by fair dice roll.
                 //guaranteed to be random
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers, arrays, char**, i don't know...
    By landog in forum C Programming
    Replies: 6
    Last Post: 06-11-2011, 04:42 PM
  2. char pointers and arrays
    By Scramble in forum C Programming
    Replies: 3
    Last Post: 06-30-2010, 04:17 AM
  3. Pointers & char arrays
    By lautarox in forum C Programming
    Replies: 11
    Last Post: 05-13-2009, 08:49 PM
  4. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  5. use of pointers with char arrays
    By txp200 in forum C Programming
    Replies: 6
    Last Post: 05-12-2004, 06:52 PM

Tags for this Thread