Results 1 to 6 of 6

Thread: Struct and Strings in C

  1. #1
    Join Date
    Dec 2009
    Posts
    204

    Struct and Strings in C

    Hello,
    I have this following code and when I try to compile the code, the compiler gives me an error
    Here is what i try to do
    Code:
    # <stdio.h>
    Stdlib.h
    
    struct Play{
        int sp;
        chariot nm[100];
    };
    typedef struct Play Play;
    
    int hand(){
        Play ptest={0,""};
        ptest.sp=1;
        ptest.nm="Matthew";
        printf("Order Number:% d\ NPlayer Name:% s\ N",ptest.sp,ptest.nm);
        return 0;
    }
    Please check my code if you find any errors then guide me with the correct code. Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    347

    Re: Struct and Strings in C

    Hello,
    If I am not wrong the string type does not exist in C.
    Here is what i am trying to explain
    Code:
    ptest.name="Matthew";
    To copy a string of characters into another character string, use the function strcpy.
    Code:
    int hand(){
        Play ptest={0,""};
        ptest.sp=1;
        ptest.nm="Matthew";
        printf("Order Number:% d\ NPlayer Name:% s\ N",ptest.sp,ptest.nm);
        return 0;
    }
    Please correct your code and then try to compile the program. I hope this solution works.

  3. #3
    Join Date
    Nov 2009
    Posts
    359

    Re: Struct and Strings in C

    Hello,
    Take a look at the following code
    Code:
    Player ptest={0,""}; / / Struct
        ptest.name="Matthew"; / / No right
    I think this is what you have mistaken in your code. If you make the necessary changes in your code, then I guess the code will work properly.
    Code:
    Player ptest={1,"Matthew"};
    That's right this is what I am taking about, the above line in your code should be changed.

  4. #4
    Join Date
    Nov 2009
    Posts
    446

    Re: Struct and Strings in C

    Hello,
    There is no type string in C. There are only literal strings "test" Which have a type Table N Where N is the length of the chain, including '\0' Final (So "test" is type Table 7 chariot.).
    So, we can write it as
    Code:
    printf("Hello, world\ N");
    and in this case the string is a static array anonymous, and it is implicitly converted to a pointer to its first element under the pointer conversion tables (which fell much since the prototype printf is int printf (const char *...). Hope this information will help you.

  5. #5
    Join Date
    Nov 2009
    Posts
    330

    Re: Struct and Strings in C

    Hello,
    I agree with the above post, we can also write it as
    Code:
    char t[64] = Hello;
    and in this case, the string literal is used as initializer array. However, we have no right to write
    Code:
    chariot t[64]; t = Hello;
    because in this case, as with printf the string is a static array anonymous, and it is implicitly converted to a pointer to its first element under the pointer conversion tables, and we have no right to assign a pointer to an array.
    But we can define in this alternative way
    Code:
    char *p; p = Hello;
    since p is a pointer, and Hello is implicitly converted to pointer.

  6. #6
    Join Date
    Dec 2009
    Posts
    204

    Re: Struct and Strings in C

    Hello,
    Hence the "" "" around "" string type "(as heard, an array of floats of course). By replacing the "ptest.name =..." by "strcpy (ptest.name,"name");" it works well. Thanks you guys for helping me. Also, if there is an another solution for doing the same then I am the interested one here. So, if you have some alternate examples then please let me know or post them, so that I can study the sample codes.

Similar Threads

  1. How to use Strings in Python
    By Edmund-Windows in forum Software Development
    Replies: 5
    Last Post: 31-12-2010, 06:25 AM
  2. Strings in JavaFX
    By Messenger in forum Software Development
    Replies: 3
    Last Post: 16-07-2010, 04:57 PM
  3. Enums VS Struct
    By Rum in forum Software Development
    Replies: 5
    Last Post: 28-01-2010, 08:56 AM
  4. C++ struct vs class
    By avvia in forum Software Development
    Replies: 3
    Last Post: 13-07-2009, 10:53 AM
  5. Array of Strings in C++
    By Jaden in forum Software Development
    Replies: 3
    Last Post: 25-10-2008, 02:18 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,656,315,771.15184 seconds with 17 queries