Doubts in the strcpy function used - c

I am using two buffers which are of unsigned char and when I used the function strcpy, there is a compilation error which says "invalid conversion of unsigned char * to char *". Can anyone please tell me what difference does an unsigned buffer vs. signed buffer makes to the output? This is the program which I had tried.
main()
{
unsigned char buff[20] = "Michael";
unsigned char dst[20] = "Jackson";
strcpy(buff,dst);
printf("The string is %s\n",buff);
}
Now when i typecast the parametrs passed in strcpy to (char *),This programe works fine as shown below
main()
{
unsigned char buff[20] = "Michael";
unsigned char dst[20] = "Jackson";
strcpy((char *)buff,(char *)dst);
printf("The string is %s\n",buff);
}
2nd Question: Does typecasting to char* in the string function create any issues?
Please do let me know if you need any more inputs.

You can just change:
unsigned char buff[20] = "Michael";
unsigned char dst[20] = "Jackson";
to
char buff[20] = "Michael";
char dst[20] = "Jackson";
To think of it logically, A string cannot be signed or unsigned, it is not a numeric value to be treated that way, it is just an char array and you should declare and use it so.
Why do you get the error?
strcpy has the prototype:
char *strcpy(char *restrict s1, const char *restrict s2);
And what you are passing to it is, unsigned char*, when a function is called parameter type checking takes place and the compiler finds that you are not calling the function with proper type parameters and hence it complains about it, When you apply a cast the function arguments match and hence compilation passes.
Does typecasting to char in the string function create any issues?*
No, in this case it doesn't.
Though it makes more sense to change your usage of unsigned char for the reasons mentioned above in the answer.
Suggestion:
Strcpy is not safe, so you are much better off using strncpy, it allows you to explicitly specify how many characters to copy, rather than rely on null terminator of the source string, Also this helps avoiding buffer overflow since you explicitly specify a length.

The way you are using it should be fine.
But you shouldn't use unsigned char arrays with string handling functions. In C strings are char arrays, not unsigned char arrays. Since passing to strcpy discards the unsigned qualifier, the compiler warns.
As a general rule, don't make things unsigned when you don't have to.

strcpy wants char *'s, plain and simple. Cast your buffers to char *'s and you'll be fine. Then there's the whole safety problem with using strcpy in the first place...

Related

convert from unsigned char to char*

I have a function that currently returns unsigned char. However, I want to cast that unsigned char to a char* but I keep getting cast to/from pointer from/to integer of different size warning.
Let foo be the function that return an unsigned char
I tried something like this:
char* convert = (char*)foo();
The reason for converting to char* is because I need to use the strcasecmp function to perform a string compare.
To achieve what you want, use the following code:
char array [2];
array [0] = (char) foo ();
array [1] = '\0';
...
... strcasecmp (array, ...);
This builds an array containing a string of a single character which you can then use.
You can't cast a non-pointer function return value into a pointer at all. Think about it, where would this pointer point to?
You can, however, save the result to a variable and have a pointer to it.
For example,
char tmp = (char) foo();
char * ptr = &tmp;
Edit:
As another answer said, if you want to use strcasecmp you would need a char array where the last byte is the null terminator.

c4133 warning c code

I have a problem with warning c4133, it say that the problem is incopatible types from char* to int*, I try to cast the pointer ((char*)x) but with no luck, maybe someone knows whats the problem/
this is my program, the problem in the function.
void replaceSubstring(char *str, char *substr)//function that gets string and substring and make the substring in string big letters
{
int i;
int *x;
x = (strstr(str, substr));//The problem line
while (strstr(str,substr) != NULL)
{
for (i=0;i<strlen(substr);i++)
{
*x = *x - 32;
x++;//move to the next char
}
x = (strstr(str, substr)); //first apear of substr int str
}
}
in your code, x is defined as int * but the return type of strstr() is char *.. Check the man page here.
Worthy to mention, casting is usually considered a bad practice in c. With properly written code, most of the cases, casting can be avoided. Most of the cases, casting may introduce lot of bugs. Double check the data types and stay away from casting.
Sidenote: Just a suggestion, maybe before directly subtracting 32 from *x, maybe you want to perform a range check on *x to be within 97-122 to ensure that is a lower-case letter.
Also, better to perform strlen(substr) outside the loop, store into a variable and use that value in looping. Will save you the overhead of redundant calls to strlen().
Prototype of strstr is
char *strstr(const char *haystack, const char *needle);
Return value is character pointer. So make the x as a character pointer.

Adding a null terminator to a string

I have used sscanf to scan in a string and add it to a structure. The only problem is that I cannot print out the string because there is no null terminator added by default. I tried to add a null in there with the strcat() function but came to the realization this cannot work. Thanks for any help.
struct a
{
int *ref;
char string[50];
}rec;
void task()
{
char *p_test;
char test[50] = "9999:STRING OF TEXT";
p_test = test;
sscanf(p_test, "%d:%[^\n]", &rec.ref, rec.string);
printf("String is:%s", &rec.string);
}
There are multiple problems with your code.
test[50] = "9999:STRING OF TEXT";
This is wrong for two reasons.
A) test is an array of char, not an array of char*. So, when you assign a string (a char*) to an element, the address is converted to a char.
B) Element 50 does not exist in your array, and writing to it invokes undefined behavior. You have an array of 50 elements with indices 0...49.
To assign an initial value to your array, all you need do is:
char test[50] = "9999:STRING OF TEXT";
And since test does not need to be modified...
const char *test = "9999:STRING OF TEXT";
If you want to zero an array, the simplest method is:
char test[50] = {0};
Of course, you don't need to if you assign the string properly to begin with, and this is not your problem.
Your string member of the a struct is a char, not a char* (a string), and using the %s format specifier when printing it invokes undefined behavior.
main is defined to return int, not void.
sscanf expects pointers to data to fill in. rec.ref is an int, you need to pass its address, i.e., &rec.ref.
You need to allocate storage for rec.string.
Here is a working example:
#include <stdio.h>
#define STRING_LEN 50
struct a
{
int ref;
char string[STRING_LEN];
} rec;
int main()
{
char test[STRING_LEN] = "9999:STRING OF TEXT";
// note that, in the real world, this may
// be a buffer overflow waiting to happen
sscanf(test, "%d:%[^\n]", &rec.ref, rec.string);
printf("String is:%s, rec is:%d", rec.string, rec.ref);
return 0;
}
You have a number of problems; a lack of null-terminator actually isn't one of them.
One problem is a syntax error; this:
char test[50]
test[50] = "9999:STRING OF TEXT";
is not valid syntax. You need:
char test[50] = "9999:STRING OF TEXT";
Another problem is that rec doesn't have a string (a char * or char[]), it just has a single character (a char). A simple approach, to get you started, is:
struct a
{
int ref;
char string[50];
} rec;
A third problem is that sscanf's arguments all have to be pointers. C is a pass-by-value language; if you just pass in an integer, that doesn't give sscanf any way to modify that integer:
sscanf(test, "%d:%[\n]", &rec.ref, rec.string); // (once rec.string is a pointer)
A fourth problem is that the format-specifier %[\n] does not mean what you seem to want it to mean. (Maybe you actually wanted %[^\n]?)
Other, smaller issues include your return-type for main (it should be int, not void).
The common thread with most of these issues is that your compiler would have eagerly helped you identify them, had you simply turned on compiler warnings!

how to set char array to string in c

so i have a struct call Process_Info
struct Process_Info {
char name[128];
int pid;
int parent_pid;
int priority;
int status;
};
and an array of Process_Info call info. I set pid in info to an integer, it works but when I try to set name in info to "{kernel}" like this
info[i].name="{kernel}";
and it give me incompatible type in assignment error. I search online it seem i can do this, like in http://www.cs.bu.edu/teaching/cpp/string/array-vs-ptr/, they did char label[] = "Single"; So what am i doing wrong?
The short answer: A C compiler will bake constant strings into the binary, so you need to use strncpy (or strcpy if you aren't worried about security) to copy "{kernel}" into info[i].name.
The longer answer: Whenever you write
char label[] = "Single";
the C compiler will bake the string "Single" into the binary it produces, and make label into a pointer to that string. In C language terms, "Single" is of type const char * and thus cannot be changed in any way. However, you cannot assign a const char * to a char *, since a char * can be modified.
In other words, you cannot write
char label[] = "Single";
label[0] = "T";
because the compiler won't allow the second line. However, you can change info[i].name by writing something like
info[i].name[0] = '[';
because info[i].name if of type char *. To solve this problem, you should use strncpy (I referenced a manual page above) to copy the string "{Kernel}" into info[i].name as
strncpy(info[i].name, "{Kernel}", 256);
info[i].name[255] = '\0';
which will ensure that you don't overflow the buffer.
I think you may be mistaken. The way you would assign this would be one char at a time like so...
name[] = {'a','b','c','d'};

Question about weird pointer

I study C. Digging in some C source code where I found that line. I have read about pointers, but I did not see such an example.
char *uppercase (char *s);
What that mean ?
It's a declaration of a function that takes a char pointer and returns a char pointer.
That means that the function takes a pointer to a character and returns a pointer to a character i.e the start of the string.
char *uppercase (char *s);
is the same as
char* uppercase (char *s);
Its return type is char*; it returns a pointer.
uppercase is afunction which returns a char type address ( that is, it can be stored in a char pointer ) as its written as char *uppercase...
uppercase() takes a char pointer as argument char *s...
therefore its written as char *uppercase( char *s).
In C the location of the * doesn't matter as long as it is somewhere between the type and name. So char* s is the same as char *s and even char * s.
The same applies to functions and their return types, char* uppercase() is equivalent to char *uppercase() and char * uppercase().
White-space is more or less ignored in C so when writing your own code I recommend you pick one format and stick with it.
The location of the asterisk * can be anywhere: between return type or function name.
It is more logical to keep it on the return type's end i.e. char*

Resources