Thread: Pointer index wierd output:

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    11

    Pointer index wierd output:

    This gives me a blank space when I compile and run... what's going on?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      char *array;
      array = malloc(sizeof(char)*4);
      *array = 'a';
      *array++ = 'b';
      *array++ = 'c';
      *array++ = 'd';
      printf("%s\n", array-4);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You should learn how to "be the computer" and trace your code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      char *array;
      array = malloc(sizeof(char)*4);
      *array = 'a';  //original array is now "a???"
      *array++ = 'b';  //original array is now "b???"
      *array++ = 'c';  //original array is now "bc??"
      *array++ = 'd';  //original array is now "bcd?"
      printf("%s\n", array-4);  //prints the string starting one character before the actual malloced memory
    }

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    11
    Quote Originally Posted by tabstop View Post
    You should learn how to "be the computer" and trace your code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      char *array;
      array = malloc(sizeof(char)*4);
      *array = 'a';  //original array is now "a???"
      *array++ = 'b';  //original array is now "b???"
      *array++ = 'c';  //original array is now "bc??"
      *array++ = 'd';  //original array is now "bcd?"
      printf("%s\n", array-4);  //prints the string starting one character before the actual malloced memory
    }
    I did try to trace it, I spent a long time playing around trying to fix it. I don't get why *array++ = b makes it b??? instead of ab???.
    How can *array and *array++ possibly refer to the same position I mean ++ should increment it by 1?

    Also why does -4 give me a blank I mean it should at least go 3 chars into the array?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It should increment it by 1 AFTER everything else has happened. (That's why the language also contains *++array.) In this case "everything else" would be assigning to the dereferenced pointer.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    11
    Quote Originally Posted by tabstop View Post
    It should increment it by 1 AFTER everything else has happened. (That's why the language also contains *++array.) In this case "everything else" would be assigning to the dereferenced pointer.
    Ok it works if I switch the ++ positions, but then I tried using (1+array) instead of (++array), now my output is just blank?

    Not sure what you mean by everything else assigned to the dereferenced pointer.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    printf works with the nul-terminated strings. your string has no nul-termination, and no place for nul-terminator. so it cannot be manipulated using any C-string routines
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Configure View Post
    Also why does -4 give me a blank I mean it should at least go 3 chars into the array?
    If you're unlucky enough, the 1 character before your array is a NULL character. Change your printf to something like this to see what's going on:
    Code:
    printf ("->&#37;s<-\n", array-4);
    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Hazy Pointer output [:(]
    By C-RaT in forum C Programming
    Replies: 12
    Last Post: 06-16-2008, 10:26 AM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  5. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM