Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: I need help

Reply
Thread Tools

Re: I need help

 
 
janus
Guest
Posts: n/a
 
      05-16-2011

> --
> Ian Collins

You must not use the macro STRNCAT in e.g. this case:

void a_procedure(char *a)
{
STRNCAT(a, "Jersey");
}

int main(void)
{

char string[10] = "NEW";
a_procedure(string);
}

Here sizeof(a) is the size of a pointer and not the size of string. Instead
you could do it without the macro:

void a_procedure(char *a, size_t size)
{
strncat(a, "Jersey", size - strlen(a) - 1);
}

int main(void)
{

char string[10] = "NEW";
a_procedure(string, sizeof(string))

On Monday, May 16, 2011 11:26:30 AM UTC+1, Heinrich Wolf wrote:
> "Ian Collins" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:(E-Mail Removed)...
> > On 05/15/11 06:29 AM, Heinrich Wolf wrote:
> >>
> >> or with a macro:
> >>
> >> #define STRNCAT(a, b) strncat(a, b, sizeof(a) - strlen(a) - 1)
> >> char string[10] = "NEW";
> >> STRNCAT(string, "Jersey");

> >
> > That macro would go horribly (and silently) wrong if
> >
> > a) 'a' wasn't a fixed sized array, or
> > b) it was, but the definition wasn't visible when the macro was expanded.
> >
> > --
> > Ian Collins

>
> You must not use the macro STRNCAT in e.g. this case:
>
> void a_procedure(char *a)
> {
> STRNCAT(a, "Jersey");
> }
>
> int main(void)
> {
>
> char string[10] = "NEW";
> a_procedure(string);
> }
>
> Here sizeof(a) is the size of a pointer and not the size of string. Instead
> you could do it without the macro:
>
> void a_procedure(char *a, size_t size)
> {
> strncat(a, "Jersey", size - strlen(a) - 1);
> }
>
> int main(void)
> {
>
> char string[10] = "NEW";
> a_procedure(string, sizeof(string));
> }


I do not see any difference between the two
Janus
 
Reply With Quote
 
 
 
 
Heinrich Wolf
Guest
Posts: n/a
 
      05-16-2011

"janus" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
....
> void a_procedure(char *a)

char * is a pointer and not an array. On a 32 bit machine sizeof(a) is
normally 4 (4 bytes, 8 bit each)
....
>> char string[10] = "NEW";


sizeof(string) is 10 here.

....

> I do not see any difference between the two
> Janus


 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      05-16-2011
On 05/16/2011 06:52 AM, janus wrote:
....
> On Monday, May 16, 2011 11:26:30 AM UTC+1, Heinrich Wolf wrote:
>> "Ian Collins" <(E-Mail Removed)> schrieb im Newsbeitrag
>> news:(E-Mail Removed)...
>>> On 05/15/11 06:29 AM, Heinrich Wolf wrote:
>>>>
>>>> or with a macro:
>>>>
>>>> #define STRNCAT(a, b) strncat(a, b, sizeof(a) - strlen(a) - 1)
>>>> char string[10] = "NEW";
>>>> STRNCAT(string, "Jersey");
>>>
>>> That macro would go horribly (and silently) wrong if
>>>
>>> a) 'a' wasn't a fixed sized array, or
>>> b) it was, but the definition wasn't visible when the macro was expanded.
>>>
>>> --
>>> Ian Collins

>>
>> You must not use the macro STRNCAT in e.g. this case:
>>
>> void a_procedure(char *a)
>> {
>> STRNCAT(a, "Jersey");
>> }
>>
>> int main(void)
>> {
>>
>> char string[10] = "NEW";
>> a_procedure(string);
>> }
>>
>> Here sizeof(a) is the size of a pointer and not the size of string. Instead
>> you could do it without the macro:
>>
>> void a_procedure(char *a, size_t size)
>> {
>> strncat(a, "Jersey", size - strlen(a) - 1);
>> }
>>
>> int main(void)
>> {
>>
>> char string[10] = "NEW";
>> a_procedure(string, sizeof(string));
>> }

>
> I do not see any difference between the two
> Janus


In the first version of a_procedure, STRNCAT(a, "Jersey") expands to

strncat(a, "Jersey", sizeof(a) - strlen(a) - 1)

In that expression, sizeof(a) gives the size of the variable 'a', which
is a pointer to char. Therefore, sizeof(a) will be equal to
sizeof(char*). There's a pretty good chance that this will be either 4
or 8, depending upon what platform you're compiling for. It's unlikely
to be 10, though that's not an impossible value.

In the second version, sizeof(string) gives the size of the variable
"string", which is an array 10 char elements, so it is guaranteed to be
10. It's unlikely that this will be the same as sizeof(a); but even if
it is, that's only a coincidence.
--
James Kuyper
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-16-2011
On 05/16/11 10:52 PM, janus wrote:

Please stop starting new threads! Drop that awful new google interface.

--
Ian Collins
 
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
Help Help, I am intermediate in Java...need help in follow case ElementX Java 9 10-01-2008 08:02 PM
Help Help. I really need some help with this =?Utf-8?B?Q2hyaXM=?= ASP .Net 3 01-31-2007 09:33 PM
re_---need help Network Adapters!!!! NEED HELP!!!! hedayatniac@gmail.com Computer Support 4 08-13-2006 01:03 AM
Need help! I need to add lead zeros to a textbox Teep ASP .Net 2 06-21-2004 01:04 PM
Please help!!! Need datagrid selection to fill textboxes...Need quick!! TN Bella ASP .Net 1 06-18-2004 01:31 AM



Advertisments