-1

int main() takes 1 command line argument: in_filename = argv[1] (command line argument example: inputfile.txt). *in_filename and *out_filename are both const char *. I want to write at out_filename address the value stored in the address pointed by *in_filename. For example if *in_filname is 0x7fffffffe930 "inputfile.txt" at *out_filename pointed address (different from 0x7fffffffe930) will be stored "inputfile.txt".

int main(int argc, char **argv){

const char *in_filename, *out_filename;
in_filename  = argv[1];
out_filename = argv[1];
return 0
 }

If I use the code above out_filename will have the same address as in_filename but I want him to have a different address and the same value (inputfile.txt) as the one stored at the address pointed by in_filename.

If I use

int main(int argc, char **argv){

const char *in_filename, *out_filename;
in_filename  = argv[1];
strcpy(out_filename, in_filename);
return 0
 }

I receive in Debugger I receive the error: out_filename has an address 0x40000 but there is an error accessing it and the program stops. in_filename has a adress and the correct value stored at it. What is the right code for this task?

j.joe
  • 35
  • 1
  • 6
  • 2
    Please show us the complete code you have so far. And be more specific about your problem: Is it openening a file whose name you hadn't specified? Or writing a pointer's value (to a file) in a specific format? – dedObed Aug 08 '19 at 08:04
  • Do you want to copy the pointers, as in `out_filename = in_filename`? Or do you want to copy the contents of `in_filename` to where `out_filename` is pointing (after you made it point somewhere), as in `strcpy(out_filename, in_filename)`? – Some programmer dude Aug 08 '19 at 08:06
  • Thank you, I want to fullfil the second variant: o copy the contents of in_filename to where out_filename is pointing (after you made it point somewhere), as in strcpy(out_filename, in_filename). – j.joe Aug 08 '19 at 08:12
  • As a side-note, the C language actually allows you to write access the memory area of argv[1] but you can't change the size of what's allocated there. Yet another C language oddity. – Lundin Aug 08 '19 at 08:34

2 Answers2

1

The code for what you are asking is:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {

    const char *in_filename;
    char *out_filename;
    in_filename  = argv[1];
    out_filename = malloc(strlen(in_filename)+1);

    strcpy(out_filename, in_filename);

    printf("%s\n", in_filename);
    printf("%s\n", out_filename);

    return 0;
}

The function strcpy takes a char * as the destination, not a const char * see in strcpy.

Giampietro Seu
  • 786
  • 9
  • 16
  • Thank you! It is essential that out_filename has the format of const char* so I changed out_filename = (char *) malloc(strlen(in_filename)+1); to out_filename = (const char *) malloc(strlen(in_filename)+1); – j.joe Aug 08 '19 at 08:26
  • *" It is essential that out_filename has the format of const char*"* - no, it isn't. You can pass it to any function requiring `const char*` as-is. Also, though it matters little in this code, in general you `free` what you `malloc` once finished with it. – WhozCraig Aug 08 '19 at 08:28
  • Essential for the requirements of the code I write :) Thank you again! – j.joe Aug 08 '19 at 08:29
0

I think you are thoroughly confused.

Here, argv[1] is a pointer that points to a region of memory that is allocated by the operating system where it copies the first argument passed to your program. When you do in_filename = argv[1];, you make in_filename point to this same memory region.

Now, out_filename is also a pointer but it initially does not point anywhere. If you do in_filename = argv[1]; then both these pointers point to the previously mentioned memory region allocated by the OS, so your copy will have no meaning. But it is, of course, legal and you can do an strcpy(out_filename, in_filename).

If you want to out_filename to point to a different and valid memory region, then you need to allocate memory for it, either on the stack with:

char out_filename[strlen(argv[1]) + 1];

Or on the heap with:

out_filename = malloc(strlen(argv[1]) + 1);

Then do your copy with strcpy(out_filename, in_filename)!

th33lf
  • 2,177
  • 11
  • 15