Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Strange behaviour with printf

Reply
Thread Tools

Strange behaviour with printf

 
 
manochavishal@gmail.com
Guest
Posts: n/a
 
      03-14-2006
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.

 
Reply With Quote
 
 
 
 
pemo
Guest
Posts: n/a
 
      03-14-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
==============


 
Reply With Quote
 
 
 
 
manochavishal@gmail.com
Guest
Posts: n/a
 
      03-14-2006
>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

 
Reply With Quote
 
manochavishal@gmail.com
Guest
Posts: n/a
 
      03-14-2006
>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!!

 
Reply With Quote
 
pemo
Guest
Posts: n/a
 
      03-14-2006
(E-Mail Removed) 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
==============


 
Reply With Quote
 
manochavishal@gmail.com
Guest
Posts: n/a
 
      03-14-2006
>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

 
Reply With Quote
 
pemo
Guest
Posts: n/a
 
      03-14-2006
(E-Mail Removed) 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
==============


 
Reply With Quote
 
Rod Pemberton
Guest
Posts: n/a
 
      03-14-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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


 
Reply With Quote
 
Micah Cowan
Guest
Posts: n/a
 
      03-14-2006
"Rod Pemberton" <(E-Mail Removed)> writes:

> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
> > 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).
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[off topic]Strange Behaviour -- adding printf change the prograexecution raxit@m4mum.com C Programming 2 01-13-2009 06:54 PM
(void) printf vs printf whatluo C Programming 29 09-08-2005 05:42 PM
Strange behaviour of printf. See the code below and please tell why it behaves in this way. DeltaOne C Programming 31 05-20-2005 10:53 PM
Some strange behaviour of printf. DeltaOne C++ 4 05-18-2005 07:18 AM
bus error with printf line included, error without printf line? ben C Programming 4 06-26-2004 04:42 PM



Advertisments