Thread: pointers

  1. #1
    Registered User datainjector's Avatar
    Join Date
    Mar 2002


    okay a pointer is a variable that points to another variable address ..right ??.

    i have written thids code and it allocats 1 char right that means 1 byte

    #include <stdio.h>
    #include <stdlib.h>
    int main()
     char *sp;
     sp = malloc(1);
     strcpy(sp, "datainjector" );
          return 0;
    I allocated 1 byte space to pointer sp then it shoudl only contain d ??? Okay first of all a pointer is a varible so if i use molloc to allocate space i can use it as a variable or if i dont allocate space i can use it as a pointer ??
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    Are you ready for the Trix ???

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
     char *sp;
     sp = malloc(1);
     strcpy(sp, "datainjector" );
    Are you intentionally trying to crash your computer, or what exactly?

    1) 'puts' is a string function.
    2) string funtions require that the string be null terminated.
    3) Thus, if you treat 'sp' as a string, it MUST contain a null. As such, a single character won't suffice.
    4) strcpy does not do any boundry checking.
    5) As such, it just merrily copies off the end of your allocated space, and over whatever happens to be there.

    1) Use strncpy.
    2) Stop trying to crash your comptuer. It'll like you better.

    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM