1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;
void decalage_1(char *s)
{
char *ptr;
ptr = s;
while (*ptr)
{
*ptr = *ptr + 1;
ptr++;
}
}
void decalage_2(string *s)
{
char *ptr;
ptr = s->c_str(); // error: invalid conversion from const char* to char*
while (*ptr)
{
*ptr = *ptr + 1;
ptr++;
}
}
int main(void)
{
char *s1 = (char *)malloc(sizeof(char) * 255);
strcpy(s1, "foo");
decalage_1(s1);
printf("%s\n", s1); // gpp
free(s1);
// ------
string *s2;
s2 = new string("bar");
decalage_2(s2);
printf("%s\n", s2->c_str());
delete s2;
return 0;
} |
Partager