-2

Hi I'm pretty new to C and still don't have a full understanding of pointers, but essentially I'm trying to copy the result of my encryption to the result pointer, and it keeps throwing various errors, currently a segmentation fault. As far as I understand, since result has been initialised as NULL, it can be changed. Any help is appreciated :)

#include <stdio.h>
#include <stdlib.h>

int pointer copy(char **result){
  char x='password';
  char* p1=&x;
  result=&p1;
  return 0; 
}

int main(void){
char *encrypted_message = NULL;
copy(&encrypted_message);
if(encrypted_message != NULL){
  printf("%s\n", encrypted_message);
  }
free(encrypted_message)
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
idkDude
  • 21
  • 1
  • 2
    A few problems: 1) It should be `*result = p1` instead; 2) `p1` is pointing to a local variable whose life-time ends with the function, the pointer will become immediately invalid as you return; 3) `char x` means that the variable `x` can only contain one single character, not a string, remember the difference between `"a"` and `'a'`; 4) Considering what you're trying to do, you need `const char *x = "password";` and `*result = x;` (no need for the `1` variable). This will work because literal strings has a lifetime of the whole program, and pointers to them will never be invalid. – Some programmer dude Aug 07 '22 at 18:45
  • 1
    What is `char x='password';`? `char` can be only single char like `'p'`. For sequence of chars you need array and double quotes `char x[]="password";` – i486 Aug 07 '22 at 18:46
  • There is a lot wrong here. To start, `char x='password';` is a single character, not a string. A string would look like `char str[] = "password";` or `const char* str = "password";`. Once you get past that you have to consider that returning a pointer to a local variable is incorrect since that variable only exists until the function ends, and `result` is passed by value so you can't change its value inside the function. You could do something like `*result = str;`, but again still making a pointer to a value that won't exist when you try to use it. – Retired Ninja Aug 07 '22 at 18:46
  • This doesn't even compile. `int pointer copy`? `char x='password'`? – bereal Aug 07 '22 at 18:46
  • 1
    There's also nothing to `free` since there's no dynamic allocation with `malloc/calloc`. – Retired Ninja Aug 07 '22 at 18:48
  • Also, to add a point 5) Only `free` what you `malloc`. If you haven't gotten a pointer from `malloc` (or other allocation function), then don't pass it to `free`. – Some programmer dude Aug 07 '22 at 18:49
  • Stack Overflow is not a tutorial site, and you have not asked a specific question. You are asking about multiple things in the code and multiple errors. Also, your code uses an identifier `pointer` that needs to be defined without showing any definition in the code. Readers need to see that definition to make sense of the code. – Eric Postpischil Aug 07 '22 at 19:13

2 Answers2

0

For starters there is a typo

free(encrypted_message)

You forgot to place a semicolon in the end of the statement.

Another typo is in the function name where two words are separated by a space in its declaration. And in the function call there is used another name.

This declaration

char x='password';

does not make a sense. You are trying to initialize an object of the type char with a multibyte character constant that has an implementation-defined value.

It seems you mean

char *x = "password";

Also the function parameter result is a local variable of the function. So its changing is not visible outside the function.

That is the condition of this if statement

if(encrypted_message != NULL)

will always evaluate to false.

And you may call the function free to a pointer that points to a dynamically allocated memory.

Apart from this the return type of the function int also does not make a sense.

It seems you mean the following

#include <stdio.h>

void pointer_copy(char **result){
  char *x = "password";
  *result = x;
}

int main(void){
char *encrypted_message = NULL;
pointer_copy(&encrypted_message);
if(encrypted_message != NULL){
  printf("%s\n", encrypted_message);
  }
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

You shouldn't leave blanks in the function's name. It can be pointer_copy for example.

And the pointer_copy function doesn't have to return anything so you can leave it void.

You can use strcpy if you still want to use a second pointer in the pointer_copy function. But before, don't forget to allocate memory for the pointer.

Full executable below:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


void pointer_copy(char **result)
{
    char x[10] = "password";
    char* p1 = malloc(sizeof(char));
    strcpy(p1, x);
    *result = p1;
}

int main(void)
{
    char *encrypted_message = NULL;
    pointer_copy(&encrypted_message);
    if (encrypted_message != NULL){
        printf("%s\n", encrypted_message);
    }
    return 0;
}

Even though you fix the typo, you may still want to get rid of the free(encrypted_message) statement because it gave a SIGTRAP in my compiler and we don't need it at all.

EDIT:

Ok so to avoid memory leak, let's get rid of thep1 pointer and send the encyrpted_message to the function as a pointer, allocate memory for the whole string there and free it in main.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


void pointer_copy(char **result)
{
    char x[10] = "password";
    *result = malloc(sizeof(char)*(strlen(x)+1));
    strcpy(*result, x);
}

int main(void)
{
    char *encrypted_message = NULL;
    pointer_copy(&encrypted_message);
    if (encrypted_message != NULL){
        printf("%s\n", encrypted_message);
    }
    free(encrypted_message);
    return 0;
}
  • thanks, applied that to my main code but now when I print its showing some jargon before the actual encryption, @/\xd53\xff\x7fKHOORZRUOG whith KHOORZ... being the encrypted message – idkDude Aug 07 '22 at 19:42
  • @idkDude the executable i wrote just writes `password` to my terminal, compiled with gdb with 0 errors. Did you change the `pointer_copy` function? If you still use `char x='password;'` `char* p1=&x;` statements, the `p1` doesn't take the value of `x` because it gives _integer from pointer without a cast_ error. And don't forget the syntax error about the quote which should be `"password"`. You should change the `x` with a pointer like `char *x = "password";` `*result = x;` or use `strcpy` function to copy the non-pointer string into the pointer like the code i wrote. – Ozdamar Kevser Aug 07 '22 at 20:30
  • One more thing, if you use `strcpy`, did you add the declaration `#include `? – Ozdamar Kevser Aug 07 '22 at 20:50
  • `char* p1 = malloc(sizeof(char));` allocates space for a single character. You probably want to allocate at least `strlen(x) +1` characters. There's also a memory leak. – Retired Ninja Aug 08 '22 at 00:32
  • @RetiredNinja so I fixed the memory leak, it should be working without any issues. – Ozdamar Kevser Aug 08 '22 at 07:02
  • @idkDude by the way did you just check the edited code? it should've solved your problem. – Ozdamar Kevser Aug 09 '22 at 11:34