doubt with char array

A

aki

Hi All ,

I want to assign a unsigned char pointer value to a char array.

as show below:

fun (usigned char *p1, ...)
{

char new[15];
strcpy(new,p1);// Comment on this
printf("value is strins is = %s", new)

}

Could anyone tell me , is it the right way to do it ?
Do it have any side affect.


Thanks in advance.
Aki
 
A

aki

aki said:



Why?
the reason being i want to store the value which unsigned char * is
pointing to ,into the char array.
further char array is being used ahead.
as show below:
fun (usigned char *p1, ...)

Presumably you mean unsigned char *p1
yes, sorry for the confusion.
char new[15];
strcpy(new,p1);// Comment on this

(a) the comment is non-portable - fine for C99 but not for C90 ok
(b) strcpy takes const char * for its second parameter, but you're
passing it unsigned char *. The absence of const is not a problem -
strcpy is just saying it will /treat/ the parameter as const, which
is fine - but the type difference is a big hint that you might want
to reconsider. Why do you want to pass an unsigned char * to a
function taking const char *? That's rhetorical, of course - I
don't give two hoots why you want to pass it, but it's something
/you/ need to think about more carefully. What is it about your
program design that has led you into this twisty little code? It's
a question that often needs to be faced; when we find ourselves
asking dark questions about language rules, we may instead need to
be asking simple questions about our program design.
(c) what if the string to which p1 (presumably) points has more than
14 characters before a null terminator appears?
this is taken care of , as already know the length of characters
stored at char pointer is less than array length.
I think you need to step back a bit and ask yourself why you are
doing this in the first place, and whether there is a better way to
achieve your objective, a way that doesn't involve mixing signed
and unsigned types.

yes , i understand your point.
thanks for your valued comments.
In my function, i need to do changes like :

func (char* string, unsigned char *ptr1,......)
{
char errorbuff[128];
char GT[CONST_LEN];


if(condition)
{
/* Something went wrong - */
// here i want GT should contain the value pointed by ptr1
}

sprintf(errorbuff,string,GT);


Actually to be simple , i want to know what is the good way to
assign unsigned char pointer value to a know char array ?
 
M

mark.bluemel

Hi All ,

I want to assign a unsigned char pointer value to a char array.

No. I don't think that's what you want to do at all..
as show below:

fun (usigned char *p1, ...)
{

char new[15];
strcpy(new,p1);// Comment on this

What you are doing here is copying the string that the pointer points
to into the array, not copying the pointer's value into the array.
printf("value is strins is = %s", new)

}

Could anyone tell me , is it the right way to do it  ?
Do it have any side affect.

Potential for buffer overflow is one obvious issue here. Perhaps you'd
do better to use strncpy(), or (if you're using a POSIX platform)
strdup().

I'm not sure why you feel you need to keep a copy of the string you've
been passed. Depending on how the string was produced in the first
place, it may be sufficient to simply keep a copy of the pointer
value...
 
M

mark.bluemel

(e-mail address removed) said:



Poor advice IMHO. There are lots of ways to protect your buffer that
are better than strncpy.

Fair enough. I would say in my defence that there was a "Perhaps" in
that sentence.

For my education, and Aki's, it might have been helpful to outline
some of those "lots of ways"...
 
M

mark.bluemel

In my function, i  need to do changes like :

func (char* string, unsigned char *ptr1,......)
{
  char  errorbuff[128];
  char  GT[CONST_LEN];

  if(condition)
  {
    /* Something went wrong - */
   // here i want  GT should contain the value pointed by ptr1
  }

  sprintf(errorbuff,string,GT);

Actually to be simple ,  i want to know what is the  good way to
assign unsigned char pointer value to a know char array ?

That definitely isn't what you want to do - see my previous post.

You haven't really told us enough about the set of unsigned characters
that ptr1 points to.
If it is a string, then one set of options apply, but we should
perhaps then ask why you have the "unsigned" qualifier.
If it is not a string, what is it?

Then we have the question as to why you need to copy the set of
unsigned characters - what will change between entry to the function
and wanting to use GT? Why will ptr1, or what it points to, change?
 
A

aki

In my function, i  need to do changes like :
func (char* string, unsigned char *ptr1,......)
{
  char  errorbuff[128];
  char  GT[CONST_LEN];
  if(condition)
  {
    /* Something went wrong - */
   // here i want  GT should contain the value pointed by ptr1
  }
  sprintf(errorbuff,string,GT);
Actually to be simple ,  i want to know what is the  good way to
assign unsigned char pointer value to a know char array ?

That definitely isn't what you want to do - see my previous post.

You haven't really told us enough about the set of unsigned characters
that ptr1 points to.
If it is a string, then one set of options apply, but we should
perhaps then ask why you have the "unsigned" qualifier.
If it is not a string, what is it?
It is holding some numerical value .
Then we have the question as to why you need to copy the set of
unsigned characters - what will change between entry to the function
and wanting to use GT? Why will ptr1, or what it points to, change?

In if (condition)
condition has call to a function which if successful will change the
string pointed by ptr1 .
So to be safe .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,268
Latest member
AshliMacin

Latest Threads

Top