0

tearing my hair out over C strings over here. I have a function in a project which returns a char * pointer.

I need to change an element of that string before using it. Everything I read says just use a char array with the char[] format, but this is not compatible with my project in its current state, if at all.

I have desperately been looking for someway to copy the first n characters to a second char pointer, add the updated value and then concat the remainder of the initial pointer (minus the original value that was updated) that doesnt take 40 lines of code.

Every attempt to use fgetc for this has failed to write the unsigned int to the 'updated pointer'. Do I need to sprintf the unsigned int into a char buffer or something? why does this feel so ridiculously complicated?

An example of a failed attempt at the first steps of the desired behavior:

int main() {
  char *s;
  s = "babado = lubidee = popop =pew"; 
  char * s1;
  s1 = malloc(10);
  memset(s1,'0',9);
  strcpy(s1,fgetc(s));

  for (int i = 0; i<4; i++){
    strcat(s1,fgetc(s));
  }

  printf("%s",s1);

  return 0;
}
  • 2
    looks like `sprintf()` could be part of an easy solution, more details please. **What have you tried?** what are some inputs and respective outputs? – pmg May 17 '20 at 18:07
  • We need the actual code for reference. – RobertS supports Monica Cellio May 17 '20 at 18:09
  • From your description, it sounds like you want a string replace function, but the exact details are a bit unclear. For example, you say "minus the original value that was updated", do we either know what the value is or how long that value is, or do we just know it starts at character N? The solution somewhat changes (or, simplifies) if we constrain the problem that the new value has the same character length as the original value; but maybe we can't make that assumption. I'm also unclear why you are using `fgetc` at all, as you seem to be using it incorrectly here. – Unn May 17 '20 at 18:50
  • @Unn "I'm also unclear why you are using fgetc at all" Me too. The value could be presumed, but the split its defined by position. its a character-based "bitmap" so the characters are 0's or 1's read from a text file, which I think are the same length so we do know that it starts at character n, though later to do something similar for a representation of an inode table, I will only have the entry number and not the exact character position, but Im using delimiters to segment the values because a generalized solution for variable character length doesnt seem like an easily implement solution. – RedBeansAndRice May 17 '20 at 19:33
  • i used fgetc becuase I couldnt find any simple way to split the pointer using some sort of destructuring such as newStr = oldStr[:i] + newValue + oldStr[i:] – RedBeansAndRice May 17 '20 at 19:35
  • Based on your last comment, the answers here would be helpful: https://stackoverflow.com/questions/2015901/inserting-char-string-into-another-char-string (at least for doing the line you wrote in Python in the last comment). Note you will likely be mallocing your buffer instead of declaring a static array depending on the rest of the code. – Unn May 17 '20 at 21:10

1 Answers1

1

If you need to just copy a string to another pointer,

.
.
char *newptr;
//Allocate memory for new pointer
int i=0;
while(i<n)   //first n characters
    *(newptr+i)=*(returnedptr+i);. //returnedptr is your initial ptr
  //Add new value
i++;
while( *(returnedptr+i)!='\0')
      *(newptr+i)=*(returnedptr+i);
.
.
  • 1
    thanks! I read that iterating over pointers is not a good idea for this since it will disrupt access to the original pointer so that it can't be freed later. did i misunderstand that argument possibly? – RedBeansAndRice May 17 '20 at 18:15
  • 1
    No, but in this case as the actual value of ptr s aren't changed, you can free the ptrs –  May 17 '20 at 18:18
  • Thanks! I need to add a null terminator manually at the end of this right? also when I try to pass a pointer constructed like this to a function that works fine on other char pointers with the same size, Im getting "Invalid read of size 1" and "Address 0x5324680 is 0 bytes after a block of size 4,096 (original project code) alloc'd" (even without the added null terminator). Any suggestions? – RedBeansAndRice May 17 '20 at 19:17