Thread: argv problem warning error

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    144

    argv problem warning error

    Assume, user types ./replace E XY test.txt
    All instances of the letter E will be replaced by XY in file test.txt

    The buffer is declared as char buffer[100].

    So the buffer is as below
    Code:
    buffer
     
     0   1   2   3   4  5   6   7   8   9   10
    [H]-[E]-[L]-[L]-[O]-[]-[W]-[O]-[R]-[L]-[D]
    So in this case since buffer[1] identically equal to argv[1][0],
    then set buffer[1] = argv[2] *error*

    however, I am getting this error: assignment makes integer without cast .

    How can I assign the contents of argv[2] to buffer[1]?
    Last edited by bos1234; 04-19-2012 at 03:26 AM.

  2. #2
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    argv[2] is a char *.

    buffer[1] is a char.

    You're trying to put a pointer into something that's not. Hence the error. You can either dereference the pointer (*), or you can actually try not to cram a whole given string into a single char (in this case, you're trying to replace E with XY!).

    The "without cast" part is a bit misleading but basically saying to you: "You're trying to cram something into some other type, and you haven't told me explicitly that you know what you're doing by using a cast."

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    144
    thanks.
    Is the following picture correct?
    http://i43.tinypic.com/2laaal3.png

    Should I be using malloc for the buffer? Since I'm trying to store XY into one element

  4. #4
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Yes, and maybe yes.

    You could iterate through the source buffer and each time either copy the char into a new buffer (which will hold the final result), or copy what SHOULD replace it into the new buffer. You'll have to be careful because one of those buffers will be longer than your source (and you have no idea how much longer until you get there, so it will probably keep growing - realloc - or you'll have to allocate a HUGE buffer to take account of every possibility) and you'll be at different points in each (e.g. after the first replacement, your "new" buffer will be one char longer than your old one but you still have to copy things to the END of the buffer).

    You can do without the buffers if you want to write to the output file character by character, but that's up to you.

    Literally, I'd:

    1) Read the next char.
    2) If it's the char being replaced:
    - Write the replacement string out to the file.
    Otherwise:
    - Write the char out to the file.
    3) Close the file.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    144
    I'm still finidng it difficult to compare the buffer with argv. I understand that they are different types

    I tried strcpy(argv[2], buf[x]) == 0

    but like the user above said, I'm trying to put two characters into one element

    This is the code I have written so far. Just the bare essentials. Error is on line 23

    http://codepad.org/LjkaDIeQ
    Last edited by bos1234; 04-19-2012 at 08:52 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error on sending argv
    By awiles in forum C Programming
    Replies: 4
    Last Post: 04-20-2010, 01:43 AM
  2. Problem with float and argv
    By DonFord81 in forum C Programming
    Replies: 5
    Last Post: 03-04-2009, 02:47 PM
  3. Problem with argv[]
    By switchback in forum C Programming
    Replies: 1
    Last Post: 07-27-2008, 04:51 PM
  4. Replies: 2
    Last Post: 03-26-2008, 04:44 PM
  5. Warning Error
    By aama100 in forum C++ Programming
    Replies: 13
    Last Post: 02-03-2008, 12:47 PM