Thread: Can't assign string to the string variable inside structure...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    7

    Question Can't assign string to the string variable inside structure...

    i use a structure... name customer...
    one of its member is seat_no...

    y can't i assign anything into the structure member??

    code is as shown: customer[i].seat_no="";

    no matter wat i put inside the double quote, the compiler give me an error of "incompatible types in assignment"...

    so any expert here plz help me??

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Assuming seat_no is a string variable, then use strcpy():
    strcpy(customer[i].seat_no,"3A");

    If seat_no is an int, then:
    customer[i].seat_no=3;

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    Code:
       printf("Which sale you wanna cancel?");
        scanf("%d", &cancel);
        
        for(i=0;i<20;i++)
        {
            if(cancel==customer[i].sale_no)
            {
                    customer[i].sale_no=9999;
                    strcpy(customer[i].seat_no,"zzzzzz");
                    break;
            }
        }
    this is my piece of code...

    n i already include the string.h inside my main.c

    when i debug it, the program show me "An Access Violation (Segmentation Fault) raised in your program"..

    so wat happen to my program?? i think this should work rite??

    by the way, my piece of code is just trying to remove any data inside the customer[i].seat_no...

    thanks in advance ^^

  4. #4
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    in your struct make sure you have this or something like it:
    Code:
    char seat_no[10];
    OR

    Code:
    char *seat_no;
    but for the later you need to use malloc and free.

    I recomend the former.

    Segmentation fault is basically when you put in too much information and it can't stick that information into the space you provided, much like running out of room on a line of a piece of paper, except the computer will just die on you.

    Make sure you give your strings enough room with the buffer [some_number] and make sure that some_number is bigger than the expected input (count the NULL string terminator when considering this)

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  5. #5
    root
    Join Date
    Sep 2003
    Posts
    232
    >Segmentation fault is basically when you put in too much information and it can't stick that information into the space you provided
    Or, you try to reference memory in a segment that's outside of your address space and the system doesn't like it. Hence the name, segmentation fault.

    >except the computer will just die on you
    You hope. You really really really hope.

    >I recomend the former.
    As long as you can ensure that most or all of the memory will be used. Otherwise you're just wasting space and the latter is a better choice.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    arg.............
    now tat part is working perfectly....
    but i encounter another prob....
    is it possible for me to copy a structure to another??
    i declared both structure as a same structure.....

    help will be appreciated...

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>is it possible for me to copy a structure to another??
    Yes, via simply assignment.

    struct1 = struct2;

    Both structs have to be of the same type.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    i tried struct1 = struct2..
    but devcpp tell me tat incompatible type assignment...
    n it wouldn't let me compile the program...

    so y this happen??
    how should i solve it plz~~~

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Originally posted by lcheehan
    i tried struct1 = struct2..
    but devcpp tell me tat incompatible type assignment...
    n it wouldn't let me compile the program...

    so y this happen??
    how should i solve it plz~~~
    It is much easier to help if you post enough relevant information to follow along. For example, you don't mention the types of 'struct1' and 'struct2', and you don't show the offending line of code (this is where the debugging starts). An example of a better post might be as follows.
    Code:
    #include <stdio.h>
    
    struct sCustomer
    {
       int  sale_no;
       char seat_no[32];
    };
    
    int main(void)
    {
       struct sCustomer customer[3] =
       {
          { 9999, "zzzzz"},
       };
       size_t i;
       /* display all customers */
       for ( i = 0; i < sizeof customer / sizeof *customer; ++i )
       {
          printf("customer[%d]: sale_no = %d, seat_no = \"%s\"\n",
                 (int)i, customer[i].sale_no, customer[i].seat_no);
       }
       /* copy a struct */
       customer[2] = customer[0]; /* a line in question */
       /* display all customers */
       for ( i = 0; i < sizeof customer / sizeof *customer; ++i )
       {
          printf("customer[%d]: sale_no = %d, seat_no = \"%s\"\n",
                 (int)i, customer[i].sale_no, customer[i].seat_no);
       }
       return 0;
    }
    
    /* my output
    customer[0]: sale_no = 9999, seat_no = "zzzzz"
    customer[1]: sale_no = 0, seat_no = ""
    customer[2]: sale_no = 0, seat_no = ""
    customer[0]: sale_no = 9999, seat_no = "zzzzz"
    customer[1]: sale_no = 0, seat_no = ""
    customer[2]: sale_no = 9999, seat_no = "zzzzz"
    */
    Now I could guess that in your code 'struct1' and 'struct2' have different types, or that you are not attempting to copy structs -- but rather an array member of the struct. But without seeing the code, this would remain purely a guess.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    Code:
    struct info{
        int sale_no;
        char name[50];
        char ic[15];
        char seat_no[100];
        int date;
        int time;
    }customer1[20],customer2[20],customer3[20],customer4[20],cus_temp[20];
    Code:
            switch(show)
            {
                    case 1 : cus_temp = customer1;flag=1;break;
                    
                    case 2 : cus_temp = customer2;flag=2;break;
                    
                    case 3 : cus_temp = customer3;flag=3;break;
                    
                    case 4 : cus_temp = customer4;flag=4;break;
            }
    this is the 2 code segment that i write.....

    upper is the structure declaration...

    n the below is where i try to copy two structure together....

  11. #11
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    is this information enough???
    i think this should be sufficient to do the debugging rite???

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>cus_temp = customer1
    These are both arrays of structs, you cannot assign one array to another.

    You could either use memmove() to copy the complete array or a loop to assign each element of the array.

    Looking at the code you've provided, I'd recommend you use a pointer anyway, depending on how you process the data.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Registered User
    Join Date
    Oct 2003
    Posts
    7

    Talking

    thanks pal....
    now i use the for loop to copy the sturcture n it works perfectly!!
    really appreciate all ur help....
    ^^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. assigning a string to a variable
    By xp5 in forum C Programming
    Replies: 12
    Last Post: 08-30-2007, 01:13 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Replies: 4
    Last Post: 01-22-2002, 11:13 PM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM