Hi
I'm having a very basic newbie problem,
The compiler forces me to do a strdup (because of cons correctness),
can I void the extra line in my code, and then I cant seem to update my array
after the funciton call

void fix_name(char *name){
  if(name[strlen(name)-1]!='/'){
    char *tmp = new char[(strlen(name)+1)];
    strcpy(tmp,name);
    tmp[strlen(name)] = '/';
    printf("%s\n",tmp);
    name=tmp;
  }
}

int main(int argc, char** argv){
  const char* sds = "asf/asdf";
  const char* sdsd =strdup(sds)
  printf("%s\n",sdsd);
  (fix_name(sdsd));
  printf("%s\n",sdsd);
  return 0;
}

you could do this: Also, its not necessary to surround function calls with parentheses.

int main(int argc, char** argv){
  char sdsd[] = "asf/asdf";
  printf("%s\n",sdsd);
  fix_name(sdsd);
  printf("%s\n",sdsd);
  return 0;
}

Apropos, your fix_name is wrong. It does nothing (except memory leak).

You modify dynamically allocated copy of the string argument then assign the pointer to the parameter (but not to the argument) - that's all. Now you have a memory leak (tmp is not deallocated) and the call argument is not changed...

Hmm thanks for the char[] approach.

But I cant nail down my problem with the updated cstring.
The following code

#include<iostream>
#include<cstring>

void fix_name(char *name){
  printf("pointer in function arg: %p\n",name);
  if(name[strlen(name)-1]!='/'){
    char *tmp = new char[(strlen(name)+1)];
    strcpy(tmp,name);
    tmp[strlen(name)] = '/';
    printf("content of new cstring: %s\n",tmp);
    printf("pointer of new cstring %p\n",tmp);
    name=tmp;
    printf("pointer of function arg: %p\n",name);
  }
}

int main(int argc, char** argv){
  char sdsd[] = "asf/asdf";

  printf("%s\n",sdsd);
  fix_name(sdsd);
  printf("%s\n",sdsd);
  return 0;
}

which gives rise to

./a.out
asf/asdf
pointer in function arg: 0x7fff0ef7b5e0
content of new cstring: asf/asdf/
pointer of new cstring 0x23f4010
pointer of function arg: 0x23f4010
asf/asdf

Do I need to give my function that should update my cstring a **char pointer, instead of a *char pointer?

thanks in advance

you need to actually dynamically allocate the orginal memory

#include<iostream>
#include<cstring>

void fix_name(char **name){
  printf("pointer in function arg: %p\n",name);
  int len = strlen(*name);
  if((*name)[len-1]!='/'){
    char *tmp = new char[len+2];
    strcpy(tmp,*name);
    strcat(tmp,"/");
    printf("content of new cstring: %s\n",tmp);
    printf("pointer of new cstring %p\n",tmp);
    delete[] *name;
    *name=tmp;
    printf("pointer of function arg: %p\n",*name);
  }
}

int main(int argc, char** argv){
  char* sdsd = new char[11];
  strcpy(sdsd,"asf/asdf");

  printf("%s\n",sdsd);
  fix_name(&sdsd);
  printf("%s\n",sdsd);
  delete[] sdsd;
  return 0;
}

What for this overcomplicated unclear code? What's a programming case where this nightmare required?

This thread was started from a very suspicious-looking remark The compiler forces me to do a strdup (because of cons correctness) and now we have one of the most strange snippet I have ever seen...

Thanks ancient dragon,
the trick was passing a pointer pointer then.

@arkm

The programming problem is simple.
check if a char* contains a trailing slash,
otherwise update the char*, so that it does.

I'm all ears for a more simple version,not using std::string

Thanks ancient dragon,
the trick was passing a pointer pointer then.

No -- read the code I posted carefully because there are other changes.

Yes,
you've added 'delete []', and have to dereference the double pointers.

Thanks for your help again ancient dragon.

This article has been dead for over six months. Start a new discussion instead.