Is this a good idea? (cont) 
Author Message
 Is this a good idea? (cont)

Continuing the previous topic.

The pointer to the literal (*a) is meant to be passed to an function which
 takesa char * but not a char * const as argument. Is it still safe enough
 to assume that function is not going to do anything to the vunarable
pointer?

The original post is attach :
-----------------------------------
Hi all,

I am wondering if the following code is OK. Well,
it compiles and runs OK. What I am worrying about
is the assignment from a macro to a. Is that OK?

/////////////////////////////////////
#include <stdio.h>

#define DEFAULT_NAME "hello, there!"

int main()
{
        char *a;
        a = DEFAULT_NAME;
        ^^^^^^^^^^^^^^

        (void)puts(a);
        return 0;

Quote:
}



Mon, 10 Nov 2003 21:21:19 GMT  
 Is this a good idea? (cont)

Quote:
>Continuing the previous topic.

If so, why starting a new thread?

Quote:
>The pointer to the literal (*a) is meant to be passed to an function
which
>takes a char * but not a char * const as argument. Is it still safe

enough

From the prototype point of view,
void f(char *);
and
void f(char * const);
are similar. This const is only considered in the implementation of
the function.

Quote:
>to assume that function is not going to do anything to the vunarable
>pointer?

You should post a little more than the code you posted before that
brings nothing about your new question.

--
-hs-    "spaces, not tabs" email: emdel at noos.fr
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://secure.dinkumware.com/htm_cl/
FAQ de FCLC : http://www.isty-info.uvsq.fr/~rumeau/fclc



Mon, 10 Nov 2003 21:30:14 GMT  
 Is this a good idea? (cont)

Quote:

>  The pointer to the literal (*a) is meant to be passed to an function which
>   takesa char * but not a char * const as argument. Is it still safe enough
>   to assume that function is not going to do anything to the vunarable
>  pointer?

First of all, note that a `char * const' ist a const pointer to
modifyable char, and that `const char *' (or `char const *') is a
pointer to const char.
Secondly, don't make assumptions from a function's interface; if it
says is takes a pointer to non-const char, don't pass a pointer to
const char (or to a string literal) unless the documentation most
explicitly states that it will not, under any circumstances, try to
modify the array. But such a conversion is always dangerous, and if
you try to convert a const char * to a char *, your compiler must
actually emit a diagnostic.
I'd say, make a modifyable copy of your string and pass that. This
could be as simple as changing the line

char *foo = "string literal";

to

char foo[] = "string literal";

Gergo

--
Money cannot buy love, nor even friendship.



Mon, 10 Nov 2003 21:41:56 GMT  
 Is this a good idea? (cont)

Quote:

> Continuing the previous topic.

> The pointer to the literal (*a) is meant to be passed to an function which
>  takesa char * but not a char * const as argument. Is it still safe enough
>  to assume that function is not going to do anything to the vunarable
> pointer?

> The original post is attach :
> -----------------------------------
> Hi all,

> I am wondering if the following code is OK. Well,
> it compiles and runs OK. What I am worrying about
> is the assignment from a macro to a. Is that OK?

> /////////////////////////////////////
> #include <stdio.h>

> #define DEFAULT_NAME "hello, there!"

> int main()
> {
>         char *a;
>         a = DEFAULT_NAME;
>         ^^^^^^^^^^^^^^

>         (void)puts(a);
>         return 0;
> }

According to my information the argument to puts is typed as:
  const char *
which is consistent with passing a pointer to a literal string.

  char * const
Is a different type -- the pointer is constant not what it points to.

Malcolm Kay
Malcolm Kay



Mon, 10 Nov 2003 22:39:49 GMT  
 Is this a good idea? (cont)

Quote:


>> Continuing the previous topic.

>> The pointer to the literal (*a) is meant to be passed to an function
>> which
>>  takesa char * but not a char * const as argument. Is it still safe
>>  enough to assume that function is not going to do anything to the
>>  vunarable
>> pointer?

>> The original post is attach :
>> -----------------------------------
>> Hi all,

>> I am wondering if the following code is OK. Well,
>> it compiles and runs OK. What I am worrying about
>> is the assignment from a macro to a. Is that OK?

>> /////////////////////////////////////
>> #include <stdio.h>

>> #define DEFAULT_NAME "hello, there!"

>> int main()
>> {
>>         char *a;
>>         a = DEFAULT_NAME;
>>         ^^^^^^^^^^^^^^

>>         (void)puts(a);
>>         return 0;
>> }

> According to my information the argument to puts is typed as:
>   const char *
> which is consistent with passing a pointer to a literal string.

I am not talking about puts() but a third party function.

- Show quoted text -

Quote:

>   char * const
> Is a different type -- the pointer is constant not what it points to.

> Malcolm Kay
> Malcolm Kay



Mon, 10 Nov 2003 23:43:57 GMT  
 Is this a good idea? (cont)

Quote:



> >> Continuing the previous topic.

> >> The pointer to the literal (*a) is meant to be passed to an function
> >> which
> >>  takesa char * but not a char * const as argument. Is it still safe
> >>  enough to assume that function is not going to do anything to the
> >>  vunarable
> >> pointer?

> >> The original post is attach :
> >> -----------------------------------
> >> Hi all,

> >> I am wondering if the following code is OK. Well,
> >> it compiles and runs OK. What I am worrying about
> >> is the assignment from a macro to a. Is that OK?

> >> /////////////////////////////////////
> >> #include <stdio.h>

> >> #define DEFAULT_NAME "hello, there!"

> >> int main()
> >> {
> >>         char *a;
> >>         a = DEFAULT_NAME;
> >>         ^^^^^^^^^^^^^^

> >>         (void)puts(a);
> >>         return 0;
> >> }

> > According to my information the argument to puts is typed as:
> >   const char *
> > which is consistent with passing a pointer to a literal string.

> I am not talking about puts() but a third party function.

> >   char * const
> > Is a different type -- the pointer is constant not what it points to.

Attempting to write to the string addressed by a invokes undefined
behaviour
so it would be nice to declare a with:
    const char *a;
not:
    char * const a;
which makes the pointer a constant rather than what it points to.

But (a) might later be used to point to a writable string and in this
case
needs to be declared:
    char * a;
while the programmer keeps track of where it points and whether the
string
to which it points is writable.

Whether (a) while assigned to a literal string should ever be passed as
an argument to a function declared as expecting a char * argument rather
than a const char * argument is perhaps debatable. If the documentation
gives no indication that the function will not attempt to change the
string in the circumstances then it is certainly inadvisable.

But there can be situations that lead to complications as for example
in the library function strchr, which has its first argument declared as
const char * but then returns this pointer value as char * , so that the
unwary programmer could end up attempting to change a literal string via
the returned pointer without getting a compiler diagnostic. The return
value declaration is of course designed to cover the instance of strchr
being passed a pointer to a modifiable string. If one imagines
implementing the function strchr in the C language then the
implementation
needs to cast the const char * argument to (char *) for the the return
value.

Now an implementor faced with the need to define a similar function
might choose to deal with the dilemma by declaring the argument
as char * and avoid casting the return value. (Although following
the precedent established with strchr might be a better choice)

In regard to a pointer declared as:
   char * const b;
I can see no problem in passing this to a function declared as taking
a char * argument as only the pointer value is passed, not the pointer
itself and the original pointer cannot in any case be changed by the
function.

Malcolm Kay



Tue, 11 Nov 2003 14:51:36 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. how good am I? Am I Good Enough????

2. Is this a good Idea??

3. Sorting multi column ListView -code-Any better ideas?

4. link libraries to libraries a good idea?

5. Help with ideas, best ways etc...

6. Vivianne has a good idea

7. Is this a good idea?

8. Bad trick or good idea?

9. Good program ideas?

10. Is this a good Idea??

11. Is this a good Idea??

 

 
Powered by phpBB® Forum Software