473,406 Members | 2,293 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,406 software developers and data experts.

Is this valid program

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
typedef union
{
int i;
char *s;
}union_t;
typedef struct
{
union_t union1;
int j;
}struct_t;
struct_t struct1;
struct1.union1.s = malloc(5);
strcpy(struct1.union1.s,"ASHW");
struct1.union1.s[5]='\0';
printf("%s\n",struct1.union1.s);
return 0;
}

I am getting the expected output(i.e. ASHW) for the above program.

However when a similar kind of function is tested using a testing tool
called RTRT, I am getting the following error.
" incompatible types in assignment "
The above error is for the statement where memory is allocated for a
pointer declared inside union.

The above program was compiled on Redhat Linux using GCC. The tool
(used for tesing the function) also is using the same compiler.

The question is, Can we allocate memory for a pointer declared inside a
union?

Nov 15 '05 #1
13 1671
jat
the return type of malloc is (void *)
that's why ur testing tool may be giving an error.
u can explicitly cast it (char *) malloc(..) and then check out.
there is nothing wrong in assigning memory to a pointer of union
because only the value of the pointer is getting changed, the size of
union will be same.

Nov 15 '05 #2
In the function that is under test, explicit type casting is done. In
the example program I have not shown it.

Nov 15 '05 #3
va******@rediffmail.com wrote:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
int main(void)
{
typedef union
{
int i;
char *s;
}union_t;
typedef struct
{
union_t union1;
int j;
}struct_t;
struct_t struct1;
struct1.union1.s = malloc(5);
You want to check the return value of malloc here.
strcpy(struct1.union1.s,"ASHW");
struct1.union1.s[5]='\0';
Undefined behaviour. You write to memory you don't own, and
completely unnecessarily so - strcpy already appended a
terminating '\0'.
printf("%s\n",struct1.union1.s);
return 0;
}

I am getting the expected output(i.e. ASHW) for the above program.
Just bad luck.
However when a similar kind of function is tested using a testing tool
called RTRT, I am getting the following error.
" incompatible types in assignment "
The above error is for the statement where memory is allocated for a
pointer declared inside union.

The above program was compiled on Redhat Linux using GCC. The tool
(used for tesing the function) also is using the same compiler.

The question is, Can we allocate memory for a pointer declared inside a
union?


Of course. Your RTRT tool is broken, ditch it.

Best regards.
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc
Nov 15 '05 #4
Please preserve context and attribution lines when posting replies.
[Context restored]

va******@rediffmail.com wrote:
"jat" <co**************@gmail.com> wrote:
the return type of malloc is (void *)
that's why ur testing tool may be giving an error.
u can explicitly cast it (char *) malloc(..) and then check out.
In the function that is under test, explicit type casting is done. In
the example program I have not shown it.


Never, ever, cast the return value of malloc to suppress diagnostics.
Instead, write correct C code and translate it with a C compiler, not
something else.

Best regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc
Nov 15 '05 #5
jat wrote:
Provide CONTEXT. There is absolutely NO guarantee that people have seen
the message you are replying to, news groups don't work that way. If you
read yesterdays posts you will see that, like most other days, this was
stated yesterday and the instructions for using that horrible interface
known as Google Groups were posted.
the return type of malloc is (void *)
that's why ur testing tool may be giving an error.
Please don't use stupid abbreviations like ur and u, it just makes it
harder for *everyone* to read your posts.
u can explicitly cast it (char *) malloc(..) and then check out.
If you are using a C compiler then you do *not* need to cast the return
value of malloc since pointers to void are automatically converted to
pointers to other types. If you get a warning (or error) when you don't
provide the cast then you have made one of two errors:

1) You have not included stdlib.h
2) You are compiling the code as C++ where different rules apply

The OP has almost certainly committed one of these two errors in his
"similar kind of function" but seeing as the OP did not post the code
producing the error we can't say what the actual problem was.
there is nothing wrong in assigning memory to a pointer of union
because only the value of the pointer is getting changed, the size of
union will be same.


Indeed.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #6
va******@rediffmail.com wrote:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
typedef union
{
int i;
char *s;
}union_t;

typedef struct
{
union_t union1;
int j;
}struct_t;

struct_t struct1;

struct1.union1.s = malloc(5);
Here you allocate 5 bytes.
strcpy(struct1.union1.s,"ASHW");
struct1.union1.s[5]='\0';
Here you write to the non-existent byte number 6.

1) strcpy has already written the null termination in to the
destination. That is part of it's job!
2) Indexing in C starts from 0, so if you allocate 5 elements those are
elements 0 to 4 and writing beyond that invokes undefined behaviour and
leads to strange things happening, like your computer exploding when you
try to show the program to your boss.
printf("%s\n",struct1.union1.s);
return 0;
}

I am getting the expected output(i.e. ASHW) for the above program.
That's unfortunate. If you had been lucky it would have visibly crashed
due to writing past the end of your buffer.
However when a similar kind of function is tested using a testing tool
called RTRT, I am getting the following error.
" incompatible types in assignment "
The above error is for the statement where memory is allocated for a
pointer declared inside union.
Since you have not shown us the code that actually exhibits the problem,
how do you expect us to properly diagnose it?

My *guess* is that either you have not included stdlib.h in your *real*
code or you are compiling it as C++. Nothing you've posted should cause
that kind of warning.
The above program was compiled on Redhat Linux using GCC. The tool
(used for tesing the function) also is using the same compiler.

The question is, Can we allocate memory for a pointer declared inside a
union?


Of course.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #7
va******@rediffmail.com wrote:

Provide context, posts don't arrive in order and don't always arrive at
all. Check CBFalconers sig or any one of the hundreds of posts where
this has been explained and instructions given on how to work around
Googles stupidity.
In the function that is under test, explicit type casting is done. In
the example program I have not shown it.


Explicit casting is almost always wrong, especially casting from void*
(the return type of malloc) to some other pointer type. If you put in a
cast to get rid of a compiler warning without knowing *exactly* why the
compiler complained and *exactly* why a cast is needed, then that is a
good indication that all you are doing is shutting the compiler up and
creating an even bigger problem.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #8
jat wrote:
the return type of malloc is (void *)
that's why ur testing tool may be giving an error.
u can explicitly cast it (char *) malloc(..) and then check out.

And since it returns a void* you don't need to cast it.

Infact, doing so may suppress compiler diagnostics.
(e.g. if you forget to include stdlib.h)
Nov 15 '05 #9
va******@rediffmail.com wrote:
However when a similar kind of function is tested using a testing tool
called RTRT, I am getting the following error.
" incompatible types in assignment "
That's not useful. "Similar kinds" of functions might have any number
of things wrong with them. Post code that gives you a problem, not code
that is of a "similar kind."
The question is, Can we allocate memory for a pointer declared inside a
union?


Yes.
Nov 15 '05 #10
jat wrote:
the return type of malloc is (void *)
that's why ur testing tool may be giving an error.
u can explicitly cast it (char *) malloc(..) and then check out.


The above is very bad advice. Please ignore it.
Nov 15 '05 #11
va******@rediffmail.com wrote:
In the function that is under test, explicit type casting is done.
This suggests that you may have another error you have not told us
about. There is no excuse for type casting the return from malloc().
Ignore ERT when he stupidly writes that there is: He hates C and thinks
all C programs should be badly-written C++ programs.
In
the example program I have not shown it.

Nov 15 '05 #12
Martin Ambuhl <ma*****@earthlink.net> wrote:
This suggests that you may have another error you have not told us
about. There is no excuse for type casting the return from malloc().
Ignore ERT when he stupidly writes that there is: He hates C and thinks
all C programs should be badly-written C++ programs.


I have ERT killfiled, but I have not seen any evidence of his
presence in this thread.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #13
Christopher Benson-Manica wrote:
Martin Ambuhl <ma*****@earthlink.net> wrote:

This suggests that you may have another error you have not told us
about. There is no excuse for type casting the return from malloc().
Ignore ERT when he stupidly writes that there is: He hates C and thinks
all C programs should be badly-written C++ programs.

I have ERT killfiled, but I have not seen any evidence of his
presence in this thread.


Not yet, but every thread I can remember in which casting the return
from malloc() ends up with ERT chiming in with his insistence that the
only good C program is a bad C++ program. My warning was meant to be a
prophylactic.
Nov 15 '05 #14

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

Similar topics

1
by: jel | last post by:
I ran what was submitted in several forums, but it's not exactly what i'm looking for. I'm dy'n over here. Ah, the frustrations of an amateur programmer. I included the code below in c++. which...
7
by: JR | last post by:
Hey all, I have read part seven of the FAQ and searched for an answer but can not seem to find one. I am trying to do the all too common verify the data type with CIN. The code from the FAQ...
64
by: jrefactors | last post by:
Since C is a subset of C++, so any C code or C libraries (printf(), scanf(), etc...) are valid C++ code. Is that correct? so even though the whole program is written in C, but with .cpp...
23
by: James Aguilar | last post by:
Someone showed me something today that I didn't understand. This doesn't seem like it should be valid C++. Specifically, I don't understand how the commas are accepted after the function...
11
by: AC | last post by:
Is the following code valid? #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { int a; char s;
4
by: ad | last post by:
I have a xml, with a DTD to valid it: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE MyHealthData SYSTEM "Health.dtd"> How can I dertimate if a xml file is valid by program?
1
by: VM | last post by:
I'm trying to debug an application in the VC dot net compiler and when I begin, I get the error: Unable to start debugging Unable to start program 'c:\myApp\PROGRAM\myApp.exe' 'G:\Program...
13
by: joenuts | last post by:
Is it possible for a function to test one of it's passed in variables (reference to object) for validity? I would like the displayString( string &obString) function to verify that obString 1)...
5
by: nospam | last post by:
Hi all. I have encountered a "Cross-thread operation not valid" error in a test program I created and although I have came up with a solution I don't like the performance of the program. I...
1
by: danner | last post by:
I am using Visual Basic 2005 Express Edition. I have used it to create the database.. SQL Server Database file CodeDatabase.mdf My experience is outdated.. from QBasic over a decade ago. I am...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.