473,378 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Strange behaviour with printf

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 '06 #1
8 1806
>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 '06 #2
ma***********@gmail.com 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
==============
Mar 14 '06 #3
>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 '06 #4
ma***********@gmail.com 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
==============
Mar 14 '06 #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.


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 '06 #6
ma***********@gmail.com 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
==============
Mar 14 '06 #7

<ma***********@gmail.com> wrote in message
news:11**********************@v46g2000cwv.googlegr 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
Mar 14 '06 #8
"Rod Pemberton" <do*********@sorry.bitbuck.cmm> writes:
<ma***********@gmail.com> wrote in message
news:11**********************@v46g2000cwv.googlegr 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).
Mar 14 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
by: Dmitriy Iassenev | last post by:
hi, I found an interesting thing in operator behaviour in C++ : int i=1; printf("%d",i++ + i++); I think the value of the expression "i++ + i++" _must_ be 3, but all the compilers I tested...
4
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
6
by: Edd Dawson | last post by:
Hi. I have a strange problem involving the passing of command line arguments to a C program I'm writing. I tried posting this in comp.programming yesterday but someone kindly suggested that I'd...
31
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
8
by: siddharth.munshi | last post by:
union something { float a; int b; } u; int main() { printf("%d %d\n",&u.a,&u.b); u.a = 200;
23
by: gribouille | last post by:
Hi, via fgets() i create a array containing a text file. fp = fopen(argv, "r"); while ((c = fgetc(fp)) != EOF) { line = c; when i want to print it
23
by: g.ankush1 | last post by:
#include <stdio.h> /* 1st example int a() { return 1; }
20
by: Pilcrow | last post by:
This behavior seems very strange to me, but I imagine that someone will be able to 'explain' it in terms of the famous C standard. -------------------- code -----------------------------------...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.