473,785 Members | 2,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Taking a test for a job

I am applying for my first jobs after completing my PhD. I have been asked
by a company to go and take a C programming test to see how my C skills are.
Apparantly this test is mostly finding errors in small snippets of code but
I was wondering if anyone could give me tips on what kind of things I should
be looking out for. The one area I dont feel confident in is how to declare
arrays of pointers and initialising multi-dimensional arrays.

Any advice would be very welcome.

Thanks
Allan
May 26 '06
40 2191
en******@yahoo. com said:

Richard Heathfield wrote:

Not to mention the fact that array has type int *[50], so &array has type
(int *[50])*. ...


You mean int *(*)[50].


Possibly I do, yes. Personally, I shun such types, and so I lack easy
familiarity with them. For one thing, I can't see any particular benefit to
having an array of 50 pointers to int. If it were n pointers to int, that
would be a different matter.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 27 '06 #21
After adding the missing *, I don't think there is undefined behaviour
on any system just yet.

We're either writing portable C code, or non-portable C code. If you're
writing the latter, then you're on the wrong newsgroup.

The code sets all elements to all bits zero.

For unsigned char, char, and signed char, yes. For anything else, the
result is implementation-specific... and may result in Undefined
Behaviour on some platfroms.

If it's an invalid pointer value, there's not a problem until the
pointers are actually read.

Yes, but presumably, if one is setting pointer values to null, one
intends their value to be analysed at some point in the future (because
if this weren't the case, it would be pointless to set their value). When
the point comes to check for a null pointer:

if (!p)

The code is not guaranteed to work properly on all platforms.

(Sure, the code you've shown is then a
very bad idea, but don't claim UB when there isn't.)

I posted to a newsgroup which deals with the C language itself (not any
one of its particular implementations ), therefore any and all code posted
here should be fully portable. If implementation-specific behaviour may
result in Undefined Behaviour, then it doesn't belong here either. The
following code is taboo for example:

for ( int i = 1; i; ++i );

If it's a valid
null pointer, the code is unnecessarily nonportable, but correct for
that implementation.

Yet non-portable nonetheless, because the Standard explicitly indicates
that a "zero value" need not necessarily be represented by all bits zero
(for certain types).

And if all bits zero is a valid pointer value,
but not a null pointer, and that special pointer value is needed, this
may even be a good way of getting it.

Not in portable code.

In C++, I'd use a template, however, in C, a macro would probably be the
way to go:

#define ZeroArray(array ) for(unsigned long i = 0;\
i != sizeof(array) / sizeof(*array); \
++i) a[i] = 0;
(Entirely off-topic here, but if anyone's curious, here's the C++ way:
template<class T, unsigned long len>
void ZeroArray( T (&array)[len] )
{
const T * const p_last = array + (len - 1);

for ( T *p = array; ; ++p )
{
*p = T();

if ( p == p_last ) return;
}
}
-Tomás
May 27 '06 #22
Tomás wrote:
After adding the missing *, I don't think there is undefined behaviour
on any system just yet.
We're either writing portable C code, or non-portable C code. If you're
writing the latter, then you're on the wrong newsgroup.


You were talking about exams. Exams aren't necessarily only about
portable C code. (Also, see below for more.)
The code sets all elements to all bits zero.


For unsigned char, char, and signed char, yes. For anything else, the
result is implementation-specific... and may result in Undefined
Behaviour on some platfroms.


The code sets all elements to all bits zero (which may or may not be a
valid representation of any value, 0 or otherwise), regardless of its
type. If you have reason to believe otherwise, please explain.
If it's an invalid pointer value, there's not a problem until the
pointers are actually read.


Yes, but presumably, if one is setting pointer values to null,


The code doesn't necessarily set pointer values to null. You're
assuming that's the intent.
one
intends their value to be analysed at some point in the future (because
if this weren't the case, it would be pointless to set their value). When
the point comes to check for a null pointer:

if (!p)

The code is not guaranteed to work properly on all platforms.
Right, that's what I said. What you said is that there's UB even when
no such check is ever made.
(Sure, the code you've shown is then a
very bad idea, but don't claim UB when there isn't.)


I posted to a newsgroup which deals with the C language itself (not any
one of its particular implementations ), therefore any and all code posted
here should be fully portable. If implementation-specific behaviour may
result in Undefined Behaviour, then it doesn't belong here either. The
following code is taboo for example:

for ( int i = 1; i; ++i );


This newsgroup, to the best of my knowledge, is not only for strictly
conforming C code, but, exactly what you said, for the C language (as
defined by the C standard). The standard defines the behaviour for more
than strictly conforming code: it also defines the behaviour of code on
some implementations even when it may be undefined on others. If I'm
wrong here, I'd appreciate a correction from any of the more regular
users here though.
If it's a valid
null pointer, the code is unnecessarily nonportable, but correct for
that implementation.


Yet non-portable nonetheless, because the Standard explicitly indicates
that a "zero value" need not necessarily be represented by all bits zero
(for certain types).


Nit: in the normative text states that all bits zero is a valid
representation of 0 for any integer type, and that other than what the
standard guarantees, the representation of types in unspecified, but
only explicitly states all bits zero may not be a valid representation
of 0 for some types in a non-normative footnote.
And if all bits zero is a valid pointer value,
but not a null pointer, and that special pointer value is needed, this
may even be a good way of getting it.


Not in portable code.


Obviously.
In C++, I'd use a template, however, in C, a macro would probably be the
way to go:

#define ZeroArray(array ) for(unsigned long i = 0;\
i != sizeof(array) / sizeof(*array); \
++i) a[i] = 0;


I wouldn't even make it a macro. Such a simple for loop, as long as
it's formatted properly, is clear enough. I'd use a pointer instead of
an index, but that's just personal preference.

May 27 '06 #23
The code doesn't necessarily set pointer values to null. You're
assuming that's the intent.

Exactly. I was demonstarting how a not-so-good-programmer may blindly
presume that the zero value for a double is all bits zero, or that a null
pointer value is all bits zero. Such a programmer may then go on to use
memset to set each element of an array to zero. (but in reality, they're
setting each element to all bits zero -- which may or may not represent
the value 0 for that given type).

Such code is non-portable.

if (!p)

The code is not guaranteed to work properly on all platforms.


Right, that's what I said. What you said is that there's UB even when
no such check is ever made.

I may have been over-zealous with my language, but I was implying that it
would be UB because, naturally, you only set a variable's value if you're
going to read it again. Only a brain amputee would do the following:

void SomeFunc()
{
int k = 3;

/* Thirty-five lines of code here */

/* Now the last line: */

k = 2;
}

Why set k's value if you're never going to use it?

Nit: in the normative text states that all bits zero is a valid
representation of 0 for any integer type


Nit: but only for the value representation bits. There may be an extra
bit which doesn't take part in the representation of the variable's
value. Such a bit would be used for trapping. In such cases, you can't
use memset to set it to zero. The following code is non-portable:

void SetToZero( unsigned * const p )
{
memset( p, 0, sizeof(*p) );
}
#define ZeroArray(array ) for(unsigned long i = 0;\
i != sizeof(array) / sizeof(*array); \
++i) a[i] = 0;


I wouldn't even make it a macro. Such a simple for loop, as long as
it's formatted properly, is clear enough. I'd use a pointer instead of
an index, but that's just personal preference.


I too would use a pointer... but I don't see how that would be possible
when writing the macro. How would we determine the type of the pointer
variable? I think we'd need a "typeof" operator:

#define ZeroArray(array ) \
{ \
const typeof(*array) * const p_last; \
\
for( typeof(*array) *p = array; ;++p ) \
{\
*p = 0;\
if (p == p_last) break;\
}\
}
-Tomás
May 27 '06 #24

"Tomás" <No.Email@Addre ss> wrote in message
news:TU******** **********@news .indigo.ie...
After adding the missing *, I don't think there is undefined behaviour
on any system just yet.

We're either writing portable C code, or non-portable C code. If you're
writing the latter, then you're on the wrong newsgroup.


False. I'd say 40% or more of the standard is non-portable. Some examples:

bitshifts
unions
enums
any function that uses file i/o (fopen/fclose,fread,ft ell,fprintf).
printf (uses file i/o-all xxprintf variants except sprintf)
representation of null pointers, floats, integers
setjmp,jmpto,jm pbuf
time functions
signal
system
argv,argc
choice of character set
declaration of main, other than required two
whitespace or not in preprocessor directives

etc...
etc...
Rod Pemberton
May 27 '06 #25
Tomás wrote:
I may have been over-zealous with my language, but I was implying that it
would be UB because, naturally, you only set a variable's value if you're
going to read it again. Only a brain amputee would do the following:

void SomeFunc()
{
int k = 3;

/* Thirty-five lines of code here */

/* Now the last line: */

k = 2;
}

Why set k's value if you're never going to use it?


Possibly to verify the pointers are not used until they are set
properly, when debugging is done on a system which aborts when an all
bits zero pointer value is used. Not applicable to your SomeFunc
example, and not applicable to most systems, just a random thought. In
most cases, you're right, there's no reason.
Nit: in the normative text states that all bits zero is a valid
representation of 0 for any integer type


Nit: but only for the value representation bits. There may be an extra
bit which doesn't take part in the representation of the variable's
value. Such a bit would be used for trapping. In such cases, you can't
use memset to set it to zero. The following code is non-portable:

void SetToZero( unsigned * const p )
{
memset( p, 0, sizeof(*p) );
}


6.2.6.2#5:

The values of any padding bits are unspeciï¬ed.45 ) A valid (non-trap)
object representation of a signed integer type where the sign bit is
zero is a valid object representation of the corresponding unsigned
type, and shall represent the same value. *For any integer type, the
object representation where all the bits are zero shall be a
representation of the value zero in that type.*

(This was changed with C99 TC2.)
#define ZeroArray(array ) for(unsigned long i = 0;\
i != sizeof(array) / sizeof(*array); \
++i) a[i] = 0;


I wouldn't even make it a macro. Such a simple for loop, as long as
it's formatted properly, is clear enough. I'd use a pointer instead of
an index, but that's just personal preference.


I too would use a pointer... but I don't see how that would be possible
when writing the macro. How would we determine the type of the pointer
variable? I think we'd need a "typeof" operator:


Again, I wouldn't even use a macro, but when you do, good point.

May 28 '06 #26
=?utf-8?B?SGFyYWxkIHZ hbiBExLNr?= posted:
*For any integer type, the
object representation where all the bits are zero shall be a
representation of the value zero in that type.*


(I wasn't aware of this... I'll go check if it's the same for C++ aswell).

So... with regard to integer types, if the value representation bits are
all zero, then the trapping bits must be all zero too.

So that implies that the following functions all work as expected:

void SetToZero( char const * p ) { memset(p, 0, sizeof(*p)); }
void SetToZero( unsinged char const * p ) { memset(p, 0, sizeof(*p)); }
void SetToZero( int const * p ) { memset(p, 0, sizeof(*p)); }
void SetToZero( unsigned const * p ) { memset(p, 0, sizeof(*p)); }
void SetToZero( long const * p ) { memset(p, 0, sizeof(*p)); }
void SetToZero( unsigned long const * p ) { memset(p, 0, sizeof(*p)); }
void SetToZero( signed char const * p ) { memset(p, 0, sizeof(*p)); }

However, the following may not work as expected, because of the possibility
of "trapping bits":

void SetToMaxValue( unsigned long * const p )
{
memset( p, UCHAR_MAX, sizeof(*p) );
}
-Tomás
May 28 '06 #27
Tomás wrote:
=?utf-8?B?SGFyYWxkIHZ hbiBExLNr?= posted:
*For any integer type, the
object representation where all the bits are zero shall be a
representation of the value zero in that type.*
(I wasn't aware of this... I'll go check if it's the same for C++ aswell).

So... with regard to integer types, if the value representation bits are
all zero, then the trapping bits must be all zero too.


Not necessarily. All bits zero must be a valid representation of 0, but
it need not be the only one (unless there's something I'm missing, of
course).
So that implies that the following functions all work as expected:

void SetToZero( char const * p ) { memset(p, 0, sizeof(*p)); }
void SetToZero( unsinged char const * p ) { memset(p, 0, sizeof(*p)); }
void SetToZero( int const * p ) { memset(p, 0, sizeof(*p)); }
void SetToZero( unsigned const * p ) { memset(p, 0, sizeof(*p)); }
void SetToZero( long const * p ) { memset(p, 0, sizeof(*p)); }
void SetToZero( unsigned long const * p ) { memset(p, 0, sizeof(*p)); }
void SetToZero( signed char const * p ) { memset(p, 0, sizeof(*p)); }
Assuming you switch the * and const, then yes.
However, the following may not work as expected, because of the possibility
of "trapping bits":

void SetToMaxValue( unsigned long * const p )
{
memset( p, UCHAR_MAX, sizeof(*p) );
}


Indeed. There is only a guarantee for all bits zero, not any other bit
pattern.

May 28 '06 #28
"Tomas" <No.Email@Addre ss> writes:
So that implies that the following functions all work as expected:

void SetToZero( char const * p ) { memset(p, 0, sizeof(*p)); }
void SetToZero( unsinged char const * p ) { memset(p, 0, sizeof(*p)); }
void SetToZero( int const * p ) { memset(p, 0, sizeof(*p)); }
void SetToZero( unsigned const * p ) { memset(p, 0, sizeof(*p)); }
void SetToZero( long const * p ) { memset(p, 0, sizeof(*p)); }
void SetToZero( unsigned long const * p ) { memset(p, 0, sizeof(*p)); }
void SetToZero( signed char const * p ) { memset(p, 0, sizeof(*p)); }

However, the following may not work as expected, because of the possibility
of "trapping bits":

void SetToMaxValue( unsigned long * const p )
{
memset( p, UCHAR_MAX, sizeof(*p) );
}


Yes and yes.

But why are the parameter types in the former cases "pointer to
const T"? And why does it change to "const pointer to T" in the
latter case? Are these just typos or are you trying to make a
point?
--
"I'm not here to convince idiots not to be stupid.
They won't listen anyway."
--Dann Corbit
May 28 '06 #29
=?utf-8?B?SGFyYWxkIHZ hbiBExLNr?= posted:

So... with regard to integer types, if the value representation bits
are all zero, then the trapping bits must be all zero too.
Not necessarily. All bits zero must be a valid representation of 0,
but it need not be the only one (unless there's something I'm missing,
of course).

It's funny... just when you think you've thought of everything, someone
points out something that you've overlooked! Let's take an example:
An integer type which has 20 bits overall, but only 16 of them are
value representation bits. The Standard guarantees that the following
object bit-pattern MUST represent the number zero:

0000 0000 0000 0000 0000

Although there may be another way of representing the number zero for a
given integer type, we're still guaranteed that the bit-pattern displayed
above is perfectly legitimate for zero.

void SetToZero( signed char const * p ) { memset(p, 0, sizeof(*p)); }

Assuming you switch the * and const, then yes.

A typographical error, I can assure you!

Indeed. There is only a guarantee for all bits zero, not any other bit
pattern.

While we're on the topic, I just want to make sure of something.

If we have a positive integer value which can be represented accurately
in both the signed and unsigned form of an integer type, then must they
have the same object bit-pattern? (Or maybe just the same value
representation bit-pattern?). What I mean is, is the following code okay:

unsigned ConvertToUnsign ed( int const val )
{
if ( val >= 0 ) /* Make sure it's positive */
{
const unsigned * const p = (const unsigned*)&val;

return *p;
}

...
}

int ConvertToSigned ( unsigned const val )
{
unsigned const max_positive_si gned_val = INT_MAX;

if ( val <= max_positive_si gned_val ) /* Make sure within range */
{
const int * const p = (const int*)&val;

return *p;
}

...
}

The code above acts on the assumption that a given number will be stored
identically in a signed and unsigned type (assuming the actual number is
within range of both types).

Does the Standard also guarantee that one of three systems must be used
for storing negative numbers:

Sign-magnitude
One's complement
Two's complement

Or does it leave the door wide open?

-Tomás
May 28 '06 #30

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

Similar topics

9
3249
by: beliavsky | last post by:
I can define a function that transforms either a scalar or each element in a list as follows: def twice(x): try: return map(twice,x) except: return 2*x print twice(3) # 6
7
1833
by: Stephen Engle | last post by:
I am trying to allow for user account to take ownership of an Active Directory object. I have assigned the Modify Owner permission to the user on the AD object - a distribution list in this case. Using Active Directory Users and Computers, the user can take ownership of the object. But I have not been able to get the program I am working on to do so. Whenever I try to write the Security Descriptor back to the object, I get the...
2
1010
by: paul | last post by:
I'm having a weird problem now on a machine when I'm working on a project from a web server and when I rebuild that solution the coding changes do not take affect. Any changes to the .ASPX file work fine it's the changes to the .cs files that are not having any affect. I can go to another machine and refresh the project and rebuild and the changes I did on the other machine then take affect. it's like the compiled code is not being...
2
1057
by: D. Shane Fowlkes | last post by:
I'm a traditional ASP developer and am experimenting with asp.net. I installed .NET Framework 1.1 and make a quick and dirty test page called random.aspx . Can anyone tell me why this page doesn't write the value of "X" to the page AND why I can view the <% %> source code in the browser? It makes me think that my IIS doesn't recognize .aspx files. Thanks <% @Page Language = "VB" Debug = "True" Explicit = "True" %>
2
3017
by: Brian Kitt | last post by:
I have a process where I do some minimal reformating on a TAB delimited document to prepare for DTS load. This process has been running fine, but I recently made a change. I have a Full Text index on one column, and punctuation in the column was causing some problems down the line. This column is used only for full text indexing, and otherwise ignored. I decided to use the following regular expression to remove all punctuation (actually...
0
1016
by: greg weber | last post by:
I have a vb.net application that uses web services. everything was working great until i installed at a client that uses a linux proxy server running squid each user is required to authenicate when the access the net. Using IE they are prompted for username and password and go right out. When i specify the credentials and try to connect the application it waits for 100 seconds, ironically that is the timeout setting. When it comes...
3
5714
by: Roberto Hernández | last post by:
I try to use the Windows Image Acquisition (WIA) with a sample in vb.net but it takes only back photos and also at low resolution. How can I put ther resolution at 640x480? I have a Labtec webcam plus that works fine with other software. I downloaded two samples from the internte and both of them do the same problem The samples are: http://www.vbforums.com/showthread.php?t=378126 http://www.vbforums.com/attachment.php?attachmentid=44367...
3
1224
by: =?iso-8859-1?q?Erik_Wikstr=F6m?= | last post by:
I'd like to create a partial specialization of a member-method of a parameterized class which takes a parameterized argument, but I'm not sure if it's possible or, if possible, how. The following code demonstrates what I'd like to to: #include <iostream> template<class T> struct BarA {
4
7123
by: James Kanze | last post by:
On Nov 18, 5:50 pm, Pete Becker <p...@versatilecoding.comwrote: Has this changed in the latest draft. According to my copy of the standard (version 1998---out of date, I know), "The operand shall be an lvalue or a qualified-id". His expression was &Test("test2"); IMHO, the compiler generated a warning because it was being laxist.
0
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10319
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9947
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8971
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.