"strcpy(string,char[int])" doesn't work?

This is a discussion on "strcpy(string,char[int])" doesn't work? within the C Programming forums, part of the General Programming Boards category; Why doesn't it work? Is there a easy solution for this? Thanks! Source code: Code: #include <stdio.h> #include <stdlib.h> #include ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    117

    "strcpy(string,char[int])" doesn't work?

    Why doesn't it work? Is there a easy solution for this?

    Thanks!

    Source code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
        char buffer[30];
    
        //n = 12
    
        static char list[] = "H,B,C,N,P,O,S,Se,F,Cl,Br,I";
        int size = sizeof list;
    
        int i;
    
    
        for(i=0; i<size; i++)
        {
    
            strcpy(buffer,list[i]);
            printf("%s",buffer);
    
        }
    
        return 0;
    }


    Error codes:
    Code:
    C:\CPP\mypermutation2\main.c||In function 'main':|
    C:\CPP\mypermutation2\main.c|44|warning: passing argument 2 of 'strcpy' makes pointer from integer without a cast|
    c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\string.h|45|note: expected 'const char *' but argument is of type 'char'|
    ||=== Build finished: 0 errors, 1 warnings ===|

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,917
    First where is line 44?

    On line 20 of your posted code your second argument (list[i]) is a single character, you need to pass a C-string (a null character terminated character array) to strcpy(). Looking at your code it looks like you have defined your variable list incorrectly. You have defined it as a single character array but it looks like you want an array of array of characters.

    Code:
        static char list[][3] = {"H","B","C","N","P","O","S","Se","F","Cl","Br","I"};
        int size = sizeof list / 3;
    Jim

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    Woah....*mind explodes* That is.....awesome. Thank you!

    also i edited the comments out so you don't have to waste your time reading notes :P

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,917
    also i edited the comments out so you don't have to waste your time reading notes :P
    You should have recompiled your modified code and posted the new error message. The purpose of posting the complete error messages is so we can easily find the line the compiler is complaining about. The reducing of the program to the smallest complete program possible that illustrates the problem is a good thing, as long as the errors match the source.

    Jim

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    Ah ok, sorry about that. Will do that next time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nbin=fopen("input.txt","a"); doesn't work?
    By Adam Rinkleff in forum C Programming
    Replies: 2
    Last Post: 06-23-2011, 03:57 PM
  2. Replies: 4
    Last Post: 08-12-2009, 04:03 PM
  3. Replies: 4
    Last Post: 12-03-2004, 12:28 PM
  4. "return function" doesn't work
    By Cris987 in forum C++ Programming
    Replies: 10
    Last Post: 03-04-2004, 11:04 PM
  5. Why doesn't "new line" work?
    By Moron Slayer in forum C++ Programming
    Replies: 14
    Last Post: 12-22-2003, 01:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21