Strange behaviour with printf

Discussion in 'C Programming' started by manochavishal@gmail.com, Mar 14, 2006.

  1. Guest

    Hi,

    I have a structure

    MAX_ID is 5

    /**Structure for Copies*/
    typedef struct NodeVideoCopy * NodeVideoCopyPtr ;
    typedef struct NodeVideoCopy
    {
    char CopyID[MAX_ID];
    char type;
    char status;
    NodeVideoCopyPtr next;
    }NodeVideoCopy;


    /**Structure for Videos*/
    typedef struct NodeVideo * NodeVideoPtr;
    typedef struct NodeVideo
    {
    char * videoname;
    char VideoID[MAX_ID];
    int Release;
    char * director;
    unsigned int NoOfCopies;
    NodeVideoCopyPtr copies;
    NodeVideoPtr next;
    }NodeVideo;

    /*Display Videos*/
    void DisplayVideos(Library * Lib)
    {
    NodeVideoPtr current;
    NodeVideoCopyPtr copy;
    current = Lib->VideoList;
    while(current!=NULL)
    {
    printf("\nVideo Name :%s",current->videoname);
    copy = current->copies;
    printf("\nCopies are :\n");
    while(copy!=NULL)
    {
    printf("\nCopy ID :%s",copy->CopyID); /*Prinitng copies of Video*/
    printf("\tCopy type :%c",copy->type);
    printf("\tCopy Status :%c",copy->status);
    copy = copy->next;

    }

    current = current->next;
    }
    }


    Now when i am printing copies of a Video
    its printing CopyID+type+status together.


    Why is this behaviour??

    Output is like


    Video Name :Jurassic Par‼
    Copies are :

    Copy ID :C1012SA Copy type :S Copy Status :A

    Copy ID :C1013SU Copy type :S Copy Status :U

    Video Name :Sholay
    Copies are :

    Copy ID :C0001SU Copy type :S Copy Status :U

    Video Name :CockTail
    Copies are :

    Copy ID :C0005RU Copy type :R Copy Status :U

    Copy ID :C0003RU Copy type :R Copy Status :U


    Can't Underatsnd why is it going beyond the alloctaed memory i.e. 5 to
    it.
     
    , Mar 14, 2006
    #1
    1. Advertising

  2. pemo Guest

    wrote:
    > Hi,
    >
    > I have a structure
    >
    > MAX_ID is 5
    >
    > /**Structure for Copies*/
    > typedef struct NodeVideoCopy * NodeVideoCopyPtr ;
    > typedef struct NodeVideoCopy
    > {
    > char CopyID[MAX_ID];
    > char type;
    > char status;
    > NodeVideoCopyPtr next;
    > }NodeVideoCopy;
    >
    >
    > /**Structure for Videos*/
    > typedef struct NodeVideo * NodeVideoPtr;
    > typedef struct NodeVideo
    > {
    > char * videoname;
    > char VideoID[MAX_ID];
    > int Release;
    > char * director;
    > unsigned int NoOfCopies;
    > NodeVideoCopyPtr copies;
    > NodeVideoPtr next;
    > }NodeVideo;
    >
    > /*Display Videos*/
    > void DisplayVideos(Library * Lib)
    > {
    > NodeVideoPtr current;
    > NodeVideoCopyPtr copy;
    > current = Lib->VideoList;
    > while(current!=NULL)
    > {
    > printf("\nVideo Name :%s",current->videoname);
    > copy = current->copies;
    > printf("\nCopies are :\n");
    > while(copy!=NULL)
    > {
    > printf("\nCopy ID :%s",copy->CopyID); /*Prinitng copies of Video*/
    > printf("\tCopy type :%c",copy->type);
    > printf("\tCopy Status :%c",copy->status);
    > copy = copy->next;
    >
    > }
    >
    > current = current->next;
    > }
    > }
    >
    >
    > Now when i am printing copies of a Video
    > its printing CopyID+type+status together.
    >
    >
    > Why is this behaviour??
    >
    > Output is like
    >
    >
    > Video Name :Jurassic Par?
    > Copies are :
    >
    > Copy ID :C1012SA Copy type :S Copy Status :A
    >
    > Copy ID :C1013SU Copy type :S Copy Status :U
    >
    > Video Name :Sholay
    > Copies are :
    >
    > Copy ID :C0001SU Copy type :S Copy Status :U
    >
    > Video Name :CockTail
    > Copies are :
    >
    > Copy ID :C0005RU Copy type :R Copy Status :U
    >
    > Copy ID :C0003RU Copy type :R Copy Status :U
    >
    >
    > Can't Underatsnd why is it going beyond the alloctaed memory i.e. 5 to
    > it.


    The output - as far as I can tell - seems to be consistent with:

    printf("\nCopy ID :%s",copy->CopyID); /Prinitng copies of Video/
    printf("\tCopy type :%c",copy->type);
    printf("\tCopy Status :%c",copy->status);

    > Can't Underatsnd why is it going beyond the alloctaed memory i.e. 5 to
    > it.


    ???



    --
    ==============
    Not a pedant
    ==============
     
    pemo, Mar 14, 2006
    #2
    1. Advertising

  3. Guest

    >Copy ID :C1012SA Copy type :S Copy Status :A

    >Copy ID :C1013SU Copy type :S Copy Status :U


    I forgot to mention that Copy Id is actually C1012 i.e( 5 in length)
    but its also printing its copy type and status along with it.
    So its printing C1012SA instead of C1012 and why is it able to do that
    if i have declared
    CopyID[MAX_ID] with MAX_ID as 5
     
    , Mar 14, 2006
    #3
  4. Guest

    >The output - as far as I can tell - seems to be consistent with:

    > printf("\nCopy ID :%s",copy->CopyID); /Prinitng copies of Video/
    > printf("\tCopy type :%c",copy->type);
    > printf("\tCopy Status :%c",copy->status);



    Please Read the second post of mine
    I missed some detail
    Sorry!!
     
    , Mar 14, 2006
    #4
  5. pemo Guest

    wrote:
    >> Copy ID :C1012SA Copy type :S Copy Status :A

    >
    >> Copy ID :C1013SU Copy type :S Copy Status :U

    >
    > I forgot to mention that Copy Id is actually C1012 i.e( 5 in length)
    > but its also printing its copy type and status along with it.
    > So its printing C1012SA instead of C1012 and why is it able to do that
    > if i have declared
    > CopyID[MAX_ID] with MAX_ID as 5


    You've no space for a '\0' terminator then!

    And, as structure members appear in memory [ok, maybe with some padding!] in
    the order in which they appear in the struct, you're output is *now*
    understandable ... and you're lucky/unlucky that a terminator *is* being
    found - perhaps *because* of padding being added.

    --
    ==============
    Not a pedant
    ==============
     
    pemo, Mar 14, 2006
    #5
  6. Guest

    >You've no space for a '\0' terminator then!

    >And, as structure members appear in memory [ok, maybe with some padding!] in
    >the order in which they appear in the struct, you're output is *now*
    >understandable ... and you're lucky/unlucky that a terminator *is* being
    >found - perhaps *because* of padding being added.


    Thanx got it solved. Didn't came to my mind that i am missing NULL
    character.
    As the array length was full it didnt copied from strcpy().
    Hope i am correct in this.

    Thanx again
     
    , Mar 14, 2006
    #6
  7. pemo Guest

    wrote:
    >> You've no space for a '\0' terminator then!

    >
    >> And, as structure members appear in memory [ok, maybe with some
    >> padding!] in the order in which they appear in the struct, you're
    >> output is *now* understandable ... and you're lucky/unlucky that a
    >> terminator *is* being found - perhaps *because* of padding being
    >> added.

    >
    > Thanx got it solved. Didn't came to my mind that i am missing NULL
    > character.
    > As the array length was full it didnt copied from strcpy().
    > Hope i am correct in this.
    >
    > Thanx again


    strcpy copies the null terminator across - so, as long as you've increased
    MAX_ID you should be ok.

    --
    ==============
    Not a pedant
    ==============
     
    pemo, Mar 14, 2006
    #7
  8. <> wrote in message
    news:...
    > Thanx got it solved. Didn't came to my mind that i am missing NULL
    > character.


    You were missing an ASCII NUL, not a NULL. The first is a character with
    value zero, the second is a pointer constant.

    RP
     
    Rod Pemberton, Mar 14, 2006
    #8
  9. Micah Cowan Guest

    "Rod Pemberton" <> writes:

    > <> wrote in message
    > news:...
    > > Thanx got it solved. Didn't came to my mind that i am missing NULL
    > > character.

    >
    > You were missing an ASCII NUL, not a NULL. The first is a character with
    > value zero, the second is a pointer constant.


    Well, since there's no guarantee of ASCII, we really want the null
    character (which coincides with ASCII's NUL).
     
    Micah Cowan, Mar 14, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DeltaOne
    Replies:
    4
    Views:
    381
    ulrich
    May 18, 2005
  2. ben
    Replies:
    4
    Views:
    645
    Martin Ambuhl
    Jun 26, 2004
  3. DeltaOne
    Replies:
    31
    Views:
    904
    S.Tobias
    May 20, 2005
  4. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,308
  5. Replies:
    2
    Views:
    968
    jameskuyper
    Jan 13, 2009
Loading...

Share This Page