Strange behaviour with printf

M

manochavishal

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.
 
P

pemo

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.

???
 
M

manochavishal

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
 
M

manochavishal

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!!
 
P

pemo

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.
 
M

manochavishal

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
 
P

pemo

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.
 
R

Rod Pemberton

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
 
M

Micah Cowan

Rod Pemberton said:
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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top