473,786 Members | 2,583 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Char array

JS
I have this struct:

int main()
{
struct jb {
char actor[25];
struct jb *next;
};
struct jb *bond;
As I understand it actor is a char array that can hold a string with 24
chars (excluding the NULL character).

But how is it then possible to write a string larger than 25 chars, like
this:
strcpy(bond->actor, "Sean Conneryhhhhhhhh hhhhhhhhhhhh");

Nov 14 '05 #1
7 1863
In article <d3**********@n ews.net.uni-c.dk>, JS <d4***@44ada.co m> wrote:
I have this struct: int main()
{
struct jb {
char actor[25];
struct jb *next;
};
struct jb *bond; As I understand it actor is a char array that can hold a string with 24
chars (excluding the NULL character).
It can hold 24 characters followed by a NUL, or it could hold
25 characters with no NUL. (It is not mandatory for a NUL
to be in a character array -- but you have to be careful how
you use one that has no NUL.)
But how is it then possible to write a string larger than 25 chars, like
this:
strcpy(bond->actor, "Sean Conneryhhhhhhhh hhhhhhhhhhhh");


The same way that your car gas tank "can hold" a certain
amount of gas: the size does not prevent you from -trying- to
put too much in. If you try to put too much in, then the
rest spills out to somewhere else, with results that might not
matter or might be disasterous.

In your example, try writing a specific value into
the structure member next first, then do your extra-long
strcpy, and then examine what is in next afterwards.
--
"Who Leads?" / "The men who must... driven men, compelled men."
"Freak men."
"You're all freaks, sir. But you always have been freaks.
Life is a freak. That's its hope and glory." -- Alfred Bester, TSMD
Nov 14 '05 #2

JS wrote:
I have this struct:

int main()
{
struct jb {
char actor[25];
struct jb *next;
};
struct jb *bond;
As I understand it actor is a char array that can hold a string with 24 chars (excluding the NULL character).

But how is it then possible to write a string larger than 25 chars, like this:
strcpy(bond->actor, "Sean Conneryhhhhhhhh hhhhhhhhhhhh");


Writing outside the bounds of the array invokes undefined behavior.
Anything can happen. Sadly, that sometimes includes the program
seeming to function just fine. In the struct above on many platforms
there would be padding bytes between actor and next. Writing onto
those might well never have a bad effect, though it is still UB. There
are a variety of useful tools to readily detect this sort of problem
depending on what platform you are using.

In the above code, bond is never even allocated, so the bond->actor
dereference is also undefined, but perhaps all the code wasn't shown.

-David

Nov 14 '05 #3
JS wrote:
I have this struct:

int main()
{
struct jb {
char actor[25];
struct jb *next;
};
struct jb *bond;
As I understand it actor is a char array that can hold a string with 24
chars (excluding the NULL character).
Although this is pretty much correct, it is still not recommended to use
'NULL' to designate the terminating null-character. 'NULL' in C is
normally used as a null-pointer constant and has nothing to do with any
characters.
But how is it then possible to write a string larger than 25 chars, like
this:
strcpy(bond->actor, "Sean Conneryhhhhhhhh hhhhhhhhhhhh");
...


"Possible"? What exactly do you mean by this? Is it "possible" to divide
something by zero in C? Of course, one can write the code

int i = 0;
int j = 5 / i;

but nothing useful will come out of this. The same can be said about
your situation.

It is possible to perform an act of "writing" in this case, but
obviously it is not possible to fit a longer string into a 25 character
array. Formally, the behavior is undefined.

In practice, the extra characters will "stick out" of the 'actor' array.
The will either quietly overwrite the memory that immediately follows
the 'actor' array (the 'next' field in your case will get run over by
this 'strcpy' truck, and then, maybe, something else) or trigger the
platform-dependent memory-protection mechanisms, causing you program to
crash (or both).

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #4
Andrey Tarasevich <an************ **@hotmail.com> writes:
[...]
Although this is pretty much correct, it is still not recommended to use
'NULL' to designate the terminating null-character. 'NULL' in C is
normally used as a null-pointer constant and has nothing to do with any
characters.

[...]

Using NULL to denote a null character isn't "pretty much correct".
If NULL is defined as 0, it will happen to work; if NULL is defined as
(void*)0, it won't.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5
Keith Thompson wrote:
[...]
Although this is pretty much correct, it is still not recommended to use
'NULL' to designate the terminating null-character. 'NULL' in C is
normally used as a null-pointer constant and has nothing to do with any
characters.

[...]

Using NULL to denote a null character isn't "pretty much correct".
If NULL is defined as 0, it will happen to work; if NULL is defined as
(void*)0, it won't.


I never assumed that the use of 'NULL' in the OP's message was supposed
to refer specifically to the standard NULL macro. What he really meant
was clear to me from the context. The only problem I had with his
wording is that his use of 'NULL' can be _confused_ with the standard
NULL macro (by a hasteful reader). I viewed it as a mere readability
problem, not a technical mistake.

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #6
Andrey Tarasevich <an************ **@hotmail.com> writes:
Keith Thompson wrote:
[...]
Although this is pretty much correct, it is still not recommended to use
'NULL' to designate the terminating null-character. 'NULL' in C is
normally used as a null-pointer constant and has nothing to do with any
characters.

[...]

Using NULL to denote a null character isn't "pretty much correct".
If NULL is defined as 0, it will happen to work; if NULL is defined as
(void*)0, it won't.


I never assumed that the use of 'NULL' in the OP's message was supposed
to refer specifically to the standard NULL macro. What he really meant
was clear to me from the context. The only problem I had with his
wording is that his use of 'NULL' can be _confused_ with the standard
NULL macro (by a hasteful reader). I viewed it as a mere readability
problem, not a technical mistake.


It was clear to me what he meant. It was also clear to me what
mistake he was making.

The word NULL in all-caps has a very specific meaning in C (a macro
that expands to a null pointer constant). Using NULL to refer to
anything else is a bad idea. The null character can be referred to as
the null character or as NUL.

Since we don't often use all-caps in English text (unless we're
shouting), any identifier in all-caps should usually be assumed to
refer to some declared C identifier (though I just noticed that this
sentence contains an exception).

I've seen code that assigns the value NULL to an element of a
character array; it happened to work on the system in question, but it
demonstrates why it's important to keep the distinction in mind.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #7
Keith Thompson <ks***@mib.or g> wrote:
Andrey Tarasevich <an************ **@hotmail.com> writes:
[...]
Although this is pretty much correct, it is still not recommended to use
'NULL' to designate the terminating null-character. 'NULL' in C is
normally used as a null-pointer constant and has nothing to do with any
characters.


Using NULL to denote a null character isn't "pretty much correct".
If NULL is defined as 0, it will happen to work; if NULL is defined as
(void*)0, it won't.


The OP didn't write just "NULL", he wrote "the NULL character". This is
just as correct as "the NUL character": the latter is ASCII, the former
Unicode. I, too, think that this is unfortunate and confusing, but
within context it's still unambiguous. The C Standard uses the spelling
"null character", which is better.

Richard
Nov 14 '05 #8

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

Similar topics

35
20967
by: Ying Yang | last post by:
Hi, whats the difference between: char* a = new char; char* b = new char; char c ;
21
18894
by: Bret | last post by:
I'm curious why char** argv is acceptable in the main() declaration. In the comp.lang.c FAQ (question 6.18) it says that pointers to pointers and pointers to an array are not interchangable. However the declaration: int main(int argc, char** argv) is common.
19
14523
by: gaga | last post by:
I can't seem to get this to work: #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *names; char **np;
5
3980
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a variable **var make it an array of pointers? I realize that 'char **var' is a pointer to a pointer of type char (I hope). And I realize that with var, var is actually a memory address (or at
12
5530
by: arkobose | last post by:
my earlier post titled: "How to input strings of any lengths into arrays of type: char *array ?" seems to have created a confusion. therefore i paraphrase my problem below. consider the following program: #include<stdio.h> #define SIZE 1 int main()
13
2125
by: chellappa | last post by:
hi , please explain me , char pointer , char Array, char ,string... i have some many confussion about this... please clarify to me.. i need some example about this program by chellappa.ns
15
34610
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
5
7800
by: Stephen Cawood | last post by:
I'm trying to use a C++ .lib from C# (I tried the Interop group will no results). I have a working wrapper DLL (I can get back simple things like int), but I'm having issues dealing with an array of bytes. For example, the .lib contains this function: int create(int id, int scale, unsigned char *image); In the wrapper DLL I have this function:
2
7659
by: Michael | last post by:
Hi, How to understand the difference between the following three. My understanding is the number in bracket minus one is the max number of chars to store in the char array , right? Thanks in advance.
3
3495
by: ZMan | last post by:
The following code won't compile with gcc version 3.4.2 (mingw-special). How come? Error: cannot convert `char (*)' to `char**' /**********************************************************/ #include <cstdio> #define MAX_WORD_LEN 80 #define MAX_SIZE 1000
0
9655
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
9964
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
8993
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...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
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
5398
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...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.