Looking at this code from The Evils of Arduino Strings
void PrintString(const char *str) {
const char *p;
p = str;
while (*p) {
Serial.print(*p);
p++;
}
}
I was looking at reducing/compacting it. Firstly, this seems to be equivalent:
void PrintString(const char *str) {
const char *p;
p = str;
while (*p)
Serial.print(*p++);
}
Now, looking at the first two lines, how can I combine the following two lines, is it possible?
const char *p;
p = str;
Can I do this?
const char *p = str;
This would seem likely but looks unbalanced, in that there is a lack of an asterisk on the right hand side. I tried it and it seems to work but I was wondering whether it was correct, and worried that I would end up with some hard-to-track-down run-time error later on, further down the line.
However, this line below is clearly wrong (as it would change the pointer p to point to a location given by the value of the first character of the C string str):
const char *p = *str;
This is obfuscation. The original code is perfectly clear, there is no need to change anything. Your changes make it less readable.
What you can do to increase readability slightly, and this is a cosmetic change, is to use a for loop instead:
void PrintString (const char *str)
{
for(const char* p=str; *p != '\0'; p++)
{
Serial.print(*p);
}
}
Alternatively:
void PrintString (const char *str)
{
for(size_t i=0; str[i] != '\0'; i++)
{
Serial.print(str[i]);
}
}
const char *p = str;
This would seem likely but looks unbalanced, in that there is a lack of an asterisk on the right hand side.
If you separated the elements of the above snippet correctly it would not look "unbalanced" at all:
const char* is the type (a pointer to const characters)
p and str are the variables, both pointer (to const char)
You could write the above using some unconventional layout like this:
const char * /* Note the lack of a semicolon */
p = str;
The above code ends up with p being defined as a pointer to char and carrying the value of str.
You get same "result" for the below code
const char *p;
p = str;
Note that the former is called "initialisation", whereas the latter is called "assignment".
You can omit the use of the p pointer, use directly the str pointer argument, since it is a copy of the pointer to the constant content.
The changes made on str pointer don't affect the pointer used when calling the function, since the pointer is passed by value.
void PrintString(const char *str) {
while (*str) Serial.print(*str++);
}
How about replacing the while with for. Here's my take:
void PrintString(const char *str)
{
for(const char* p = str; *p; p++)
Serial.print(*p);
}
I think this could work, too:
void PrintString(const char *str) {
for(const char* p = str; *p; Serial.print(*(++p)))
;
}
As others have pointed out, we can replace p with str, since str is a local copy, just like any other variable passed as function argument.
void PrintString(const char* str) {
for( ; *str; Serial.print(*(++str)))
;
}
Related
I want to reverse a string, but I got a error on swap char.
Could someone help me with this?
char* reverse_char(char* src){
char *p,*q;
p = q = src;
while(*(p) != '\0'){
p++;
}
p--;
char temp = '\0';
while (p > q) {
temp = *p;
*p = *q; // I got exec bad access here ??? why
*q = temp;
p--;
q++;
}
return src;
}
This is the main method.
int main(int argc, char const* argv[])
{
char *hi = "hello world!\n";
printf("%s", hi);
reverse_char(hi);
printf("%s", hi);
return 0;
}
Replace char *hi = "hello world!\n"; with char hi[] = "hello world!\n";
"hello world!\n" is string literal and may not be writable causing the access error. Modifying the contents of string literal is undefined behavior, it may write the contents silently, raise an access error or may do something else unexpected. (So you should not write to string literal)
Summing up
char a[] = "..."; /* Contents of a can be assigned */
char *a = "..."; /* Writing to contents of memory pointed by a leads to UB */
Though string literals in C have types of non-const character arrays they may not be changed.
From the C Standard (6.4.5 String literals)
7 It is unspecified whether these arrays are distinct provided their
elements have the appropriate values. If the program attempts to
modify such an array, the behavior is undefined.
it is better to declare a pointer initialized with a string literal as having type const char * as it is done in C++. For example
const char *hi = "hello world!\n";
In this case instead of a run-time error you would get a compile-time error that would be understandable because the parameter of the function is declared as a non-const character pointer.
You have to use a character array instead of a pointer to a string literal.
For example
char hi[] = "hello world!\n";
As for the function that it has some problem when a pointer points to an empty string. In this case after operation
p--;
you can get invalid pointer that is not necessary less than src.
I would write the function the following way
char* reverse_string( char *s )
{
char *p = s;
while ( *p ) ++p;
if ( p != s )
{
for ( char *q = s; q < --p; ++q )
{
char c = *q;
*q = *p;
*p = c;
}
}
return s;
}
How would I append a char to string that has no initial value in the following method I've started:
void append(char a) {
const char *str;
char *ret = malloc (strlen(str)+2);
strncpy(str,strlen(str),ret);
ret[strlen(str)-2] = a;
ret[strlen(str)-1] = 0;
printf("%s", str);
}
I've tried a few different answers to other questions but none have worked, please help.
Since the pointer str is not initialized, you can't add characters to what it points at. What makes sense depends on what you're going to do next. Since you don't return a value from the function, you can't access the string to which a is appended unless append calls some other function.
char *str; is not a string. It is a mnemonic that says that *str will give you a value which is supposedly a character.
str is a pointer that points at random. Make it point to an allocated bunch of memory first.
Do something like- str = malloc(100); and then do a *(str+1) = a and then a *(str+2) = '\0' to NULL terminate the string.
how about something like:
char * append(char a, const char * str)
{
char *ret = malloc (strlen(str)+2);
strncpy(str,strlen(str),ret);
ret[strlen(str)-2] = a;
ret[strlen(str)-1] = 0;
return ret;
}
So, I have seen this strcpy implementation in C:
void strcpy1(char dest[], const char source[])
{
int i = 0;
while (1)
{
dest[i] = source[i];
if (dest[i] == '\0')
{
break;
}
i++;
}
}
Which to me, it even copies the \0 from source to destination.
And I have also seen this version:
// Move the assignment into the test
void strcpy2(char dest[], const char source[])
{
int i = 0;
while ((dest[i] = source[i]) != '\0')
{
i++;
}
}
Which to me, it will break when trying to assign \0 from source to dest.
What would be the correct option, copying \0 or not?
The code should look like as follows:
char * strcpy(char *strDest, const char *strSrc)
{
assert(strDest!=NULL && strSrc!=NULL);
char *temp = strDest;
while(*strDest++ = *strSrc++); // or while((*strDest++=*strSrc++) != '\0');
return temp;
}
You can NOT delete the second line char *temp = strDest; and directly return strDest. This will cause error for the returned content. For example, it will not return correct value (should be 22) will checking the length of returned char *.
char src_str[] = "C programming language";
char dst_str[100];
printf("dst_str: %d\n", strlen(strcpy(dst_str, src_str)));
Both copy the terminator, thus both are correct.
Note that strcpy2() does the assignment (the copying) first, then the comparison. So it will copy the terminator before realizing it did, and stopping.
Also, note that functions whose names start with str are reserved, so neither of these are actually valid as "user-level" code.
You're wrong. Both copy the \0 (NUL terminator) character. You have to copy the NUL terminator character always or your string will be broken: you'll never know when/where it ends.
Both copy the terminator, thus both are correct.
strcpy2() does the copying first, then the compares. Thus it will copy the terminator and stops.
The functions whose names start with str are reserved, so use any other variables or naming types
It is recommended not to advance the input pointers to the source and destination memory spaces, since the pointers will be used in main right away.
I've mentioned alternate methodical syntax, where in case someone might wonder the code output.
void strcpy1(char * s, char * p)
{
char * temp1 = s;
char * temp2 = p;
while(*temp1 != '\0')
{
*temp2 = *temp1;
temp1++;
temp2++;
}
*temp2 = '\0';
}
void main()
{
char * a = "Hello";
char b[10];
strcpy1(a,b);
printf("%s", b);
return 0;
}
Both strcpy1() and strcpy2() does the same. Both copy the NUL character to the end of the destination array.
Here is full implementation. You do not have to consider the \0 at the end in the first string, it will be copied automatically from the second string as per logic
//str copy function self made
char *strcpynew(char *d, char *s){
char *saved = d;
while ((*d++ = *s++) != '\0');
return saved; //returning starting address of s1
}
//default function that is run by C everytime
int main(){
//FOR STRCPY
char s1[] = "rahul"; //initializing strings
char s2[] = "arora"; //initializing strings
strcpynew(s1, s2);
printf("strcpy: %s\n", s1); //updated string after strcpy
}
You can use this code, the simpler the better !
Inside while() we copy char by char and moving pointer to the next. When the last char \0 will pass and copy while receive 0 and stop.
void StrCopy( char* _dst, const char* _src )
{
while((*_dst++ = *_src++));
}
char * strcpy(char *strDest, const char *strSrc)
{
assert(strDest!=NULL && strSrc!=NULL);
assert(strSrc + strlen(strSrc) < d || strSrc > strDest); // see note
char *temp = strDest;
while(*strDest++ = *strSrc++)
;
return temp;
}
// without the check on line 4, the new string overwrites the old including the null deliminator, causing the copy unable to stop.
Both copy the '\0'. That's what you have to do if you want to fully emulate the original strcpy
I'm trying to write strcpy on my own using pointers and I get an error during runtime.
void str_cpy(char **destination, const char *source) {
// char *s1 = *destination;
while (*source != '\0') {
**destination++ = *source++; //Get an error here
}
**destination = '\0';
}
I call the function as follows:
char *str = NULL;
str_cpy(&str, "String");
Is it not OK?
Thanks!
No, it's not okay. Why? Because str is a NULL pointer. It's pointing to nothing. When you try to write values into it, where will they go? It's not pointing to any allocated memory!
You first have to allocate memory for str. You can do:
char *str = malloc(strlen("String") + 1); // + 1 for the '\0' character at the end of C-style strings
Or you can do:
char str[256]; // make str large enough to hold 256 chars. Note that this is not as safe as the above version!
Also, destination should be a single pointer, not a double pointer. Well, it's not technically wrong to use a double pointer, it's just unnecessary.
Optionally, you can allocate the memory in the str_cpy function, like so:
void str_cpy(char **destination, const char *source) {
*destination = malloc(strlen(source) + 1);
// ... continue as normal
For simplicity's sake, this can be done in one line in a function.
void mystrcpy(char *dest, const char *src) {
while (*dest++ = *src++);
}
This being said, you do need to allocate memory for dest beforehand using malloc or just simply by having a character array like char dest[256].
I don't see any need to pass a pointer-to-pointer:
void str_cpy(char *dst, const char *src) {
while (*src != '\0') {
*dst++ = *src++;
}
*dst = '\0';
}
And you need to allocate memory for dst before passing:
const char *src = "String";
char *str = malloc(strlen(src)+1); //plus one for null byte
str_cpy(dst, src);
You should likely allocate some memory for that pointer before passing it off to a function that fills what it points to (which in this case, is NULL).
Example:
char *str = malloc(128);
if (str)
{
str_cpy(&str, "String");
free(str);
str = NULL;
}
I advise not doing this without also providing target-buffer size information (i.e. if you're writing your own, then boundary-check the target buffer, otherwise your version has the same security flaws as strcpy() which are bad enough as it is).
Note: Unless you're planning on changing the address held by the pointer passed as the target, you need not use a double pointer either. The double pointer usage you have prevents the traditional strcpy() usage pattern of:
char str[128];
str_cpy(&str, "Hello"); // error.
An array address cannot be passed as a pointer-to-pointer, so your code cannot fill a static array without an intermediate pointer:
char str[128];
char *p = str;
str_cpy(&p, "Hello"); //ok. passing address of pointer.
If this is not intentional (and I don't see why it could be unless you have ideas of internally emulating strdup() on a NULL pointer passage) You should address this.
Here is a complete implementation.
Good article from here. Describes timing and performance. I did not measure myself though.
http://www.howstuffworks.com/c35.htm
char* mystrcpy(char *dst, const char *src) {
char *ptr = dst;
while ((*dst++ = *src++) ) ;
return ptr;
}
int main(int argc, char *argv[]) {
const char *src = "This is C.\0";
char *dst = malloc(sizeof(char)*(strlen(src)+1)); //+1 for the null character
dst = mystrcpy(dst, src);
printf("%s",dst);
return 1;
}
Recently I faced same problem of above one using double pointer strcpy implementation
It might helpful to others below code
void strcpy_i( char **dst, const char *src )
{
*dst=(char *)malloc((strlen(src)+1)*sizeof(char));
char *tmp=*dst;
if(tmp == NULL || src == NULL)
return ;
while((*tmp++=*src++)!='\0');
}
int main()
{
char v[]="Vinay Hunachyal";
char *d=NULL;
strcpy_i(&d,v);
printf("%s",d);
return 0;
}
#include<stdio.h>
void main()
{
void mystrcpy(char *,char *);
char s1[100],s2[100];
char *p1;
char *p2;
p1=s1;
p2=s2;
printf("Enter the string to copy to s2...?\n");
scanf("%s",p1);
mystrcpy(p2,p1);
printf("S2 after copying = %s",p2);
}
void mystrcpy(char *p2,char *p1)
{
while(*p1!='\0')
{
*p2=*p1;
p2++;
p1++;
}
*p2='\0';
}
Its my solution..Simple to understand..
I have a simple C function which I declare as:
int strLen(const char* str_)
{
str_ = (char*) 0;
return 0;
}
I was very surprised that that compiles! Why is that?
Whereas this onedoesn't compile (which makes sense):
int str(const int i_)
{
i_ = 0;
return 0;
}
Because const char* str_ says that str_ is a pointer to const characters.
You can change str_ itself, but not what it points to.
int strLen(const char* str_)
{
str_ = (char*) 0; /* allowed */
str_[0] = '\0'; /* not allowed */
return 0;
}
In the first one, you are changing what the pointer str_ points to; the pointer str_ is not const; it just points to const data. You can make the pointer const as follows:
int strLen(const char* const str_)
This follows the conventions of C declarations,
char *str;
means that *str will give you a char (or in other words, str is a pointer-to-a-char). Similarly,
const char *str;
means that *str will give you a const char, or in other words, str is a pointer-to-a-constant-char. So, you are not allowed to change *str, but you may change str.
If you want to make str itself const, use the following syntax:
char *const str;