Regarding C pointers

This is a discussion on Regarding C pointers within the C++ Programming forums, part of the General Programming Boards category; Hi buddies, char *cTest = new char[10]; printf(" \nThe strlen(cTest) is : %d\n",strlen(cTest)); The output of this printf statement should ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    1

    Angry Regarding C pointers

    Hi buddies,

    char *cTest = new char[10];
    printf(" \nThe strlen(cTest) is : %d\n",strlen(cTest));

    The output of this printf statement should be 10 but itz giving 14.

    help please, why it so..

    regards,
    ashok

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    You arent initialising that data to anything before testing the length


    All strlen does is travel up the string until it finds a zero byte......

    When you allocate that memory, it could hold anything........only use strlen when you know there's a string in that area of data

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    It works if you use the sizeof operator and a "static" array:
    Code:
    char cTest[10];
    printf("sizeof(cTest) = %d\n", sizeof(cTest));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21