Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Array of structs with strings

Reply
Thread Tools

Array of structs with strings

 
 
Richard Heathfield
Guest
Posts: n/a
 
      03-21-2008
CBFalconer said:

> santosh wrote:
>>

> ... snip ...
>>
>> This is where a memory overwrite occurs:
>>
>> char *ptr;
>> strcpy(ptr, "hello");

>
> No it doesn't. It just blows up, because ptr was never initialized.


The behaviour is undefined. What santosh said is not unreasonable. In any
case, you describe symptoms, whereas santosh is talking about causes.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      03-21-2008
goodTweetieBird <(E-Mail Removed)> writes:

>>
>> That's very true. Try taking some rather long input and storing it in
>> robots[0].name. That way, even the best of compilers can't guess how
>> much memory should be allocated for robots[0].name to allow for
>> robots[1].name and robots[2].name. After that, output it, and you can
>> usually find that robots[1].name and robots[2].name are overwritten.

<snip>
> Sounds like a source of subtle errors.


No, you have been misinformed. Please read the other posts carefully.
If you don't understand, ask some more (and read the comp.lang.c FAQ
at http://c-faq.com/).

--
Ben.
 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      03-21-2008
On Thu, 20 Mar 2008 22:52:37 -0500, CBFalconer <(E-Mail Removed)>
wrote in comp.lang.c:

> Eric Sosman wrote:
> > goodTweetieBird wrote:
> >
> >> In C I have seen the idiom
> >>
> >> char *name = "Tweety";
> >>
> >> and have been assured that it is legit. However I have doubts about
> >> the string assignments in the example below I found on the net. Are
> >> these string assignments valid?
> >>
> >> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> >> #include <stdio.h>
> >>
> >> typedef struct robot ROBOT;
> >>
> >> struct robot {
> >> char *name;
> >> int energy;
> >> };
> >>
> >> int main() {
> >> int i;
> >>
> >> ROBOT robots[3];
> >>
> >> robots[0].name = "Lunar Lee";
> >> robots[0].energy = 50;
> >> robots[1].name = "Planetary Pete";
> >> robots[1].energy = 20;

> >
> > Yes, they are valid. `robots[n].name' is a char*
> > (for n == 0,1,2), just as `name' is, so anything you
> > can do with `name' can also be done with `robots[n].name'.

>
> You have set the pointer for name to point to a non-writable
> constant char. string. So you can't diddle the names. They are
> just there. However, you can replace them.


Correction, you have set the pointer for name to point to a string
literal, which is an unnamed array of char (NOT "const char"). Any
attempt to modify a string literal because the C standard specifically
states it does, NOT because it is const qualified.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      03-21-2008
Jack Klein wrote:
> CBFalconer <(E-Mail Removed)> wrote in comp.lang.c:
>> Eric Sosman wrote:
>>> goodTweetieBird wrote:
>>>
>>>> In C I have seen the idiom
>>>>
>>>> char *name = "Tweety";
>>>>
>>>> and have been assured that it is legit. However I have doubts about
>>>> the string assignments in the example below I found on the net. Are
>>>> these string assignments valid?
>>>>
>>>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>>> #include <stdio.h>
>>>>
>>>> typedef struct robot ROBOT;
>>>>
>>>> struct robot {
>>>> char *name;
>>>> int energy;
>>>> };
>>>>
>>>> int main() {
>>>> int i;
>>>>
>>>> ROBOT robots[3];
>>>>
>>>> robots[0].name = "Lunar Lee";
>>>> robots[0].energy = 50;
>>>> robots[1].name = "Planetary Pete";
>>>> robots[1].energy = 20;
>>>
>>> Yes, they are valid. `robots[n].name' is a char*
>>> (for n == 0,1,2), just as `name' is, so anything you
>>> can do with `name' can also be done with `robots[n].name'.

>>
>> You have set the pointer for name to point to a non-writable
>> constant char. string. So you can't diddle the names. They
>> are just there. However, you can replace them.

>
> Correction, you have set the pointer for name to point to a
> string literal, which is an unnamed array of char (NOT "const
> char"). Any attempt to modify a string literal because the C
> standard specifically states it does, NOT because it is const
> qualified.


I didn't say const. I wrote 'constant'. Do you deny that the
pointers are to constant strings?

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      03-21-2008
goodTweetieBird wrote:
> In C I have seen the idiom
>
> char *name = "Tweety";
>
> and have been assured that it is legit.


It is legit. However, the practice of pointing to non-modifiable data
with a non-const-qualified pointer in normally frowned upon, unless you
have a good reason to do so. Without such a good reason it should be

const char *name = "Tweety";

> However I have doubts about
> the string assignments in the example below I found on the net. Are
> these string assignments valid?
> ...
> robots[0].name = "Lunar Lee";
> ...


Er... Well, they are not in any way different from your first example.
Which brings the question of why you are even asking?

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Packed structs vs. unpacked structs: what's the difference? Daniel Rudy C Programming 15 04-10-2006 08:10 AM
Array of structs instead of an array with pointers to structs? Paminu C Programming 5 10-11-2005 07:18 PM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
const structs in other structs Chris Hauxwell C Programming 6 04-27-2004 07:03 PM
structs with fields that are structs Patricia Van Hise C Programming 5 04-05-2004 01:37 AM



Advertisments