Thread: assigning a string to a variable

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    33

    assigning a string to a variable

    Hi, I'm currently trying to store a string by using this notation: prvstring[STRSIZE] = str[STRSIZE]. For some reason it doesn't work. Is there a way to assign a variable equal to a string? Thank you very much!

    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main(void)
    {
    #define STRSIZE 20
    FILE *pf;
    char str[STRSIZE]; /*Maximum size of a string is 20*/
    char ustring[STRSIZE];
    char *chpoint;
    char prvstring[STRSIZE];
    
    pf = fopen("input.txt","r");
    
    if (!pf)
    {
    printf("Can't open the file\n");
    return 0;
    }
    
    printf("Enter a string. ");
    chpoint = strchr(ustring, '\n'); /*searches for "\n", address is returned if found; otherwise NULL is returned*/
    
    if (chpoint != NULL)
    {
    *chpoint = 0; /*removes the \n*/
    }
    
    while (fscanf(pf, "%s",str) == 1) /*continue to scan and print strings until the EOF*/
    {
    int v = strcmp (ustring, str);
    
    if (v == 0)
    {
    printf ("<%s> equals <%s>.\n", ustring, str);
    }
    printf("previous string is: %s \n", str);
    prvstring[STRSIZE] = str[STRSIZE];
    printf("new string is: %s\n", prvstring[STRSIZE]);
    }
    fclose(pf); /*close file*/
    return 0;
    }

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    You need to use strcpy():

    Code:
    strcpy(prvstring, str);

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    33

    segmentation fault (core dumped)

    thank you... I try your method and I got an error saying "segmentation fault (core dumped)" This is what the revised code looks like:

    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main(void)
    {
      #define STRSIZE 20
      FILE *pf;
      char str[STRSIZE];  /*Maximum size of a string is 20*/
      char ustring[STRSIZE];
      char *chpoint;
      char *prvstring;
    
      pf = fopen("input.txt","r");
    
      if (!pf)
      {
        printf("Can't open the file\n");
        return 0;
      }
    
      printf("Enter a string. ");
      chpoint = strchr(ustring, '\n');  /*searches for "\n", address is returned if found; otherwise NULL is returned*/
    
      if (chpoint != NULL)
      {
        *chpoint = 0;   /*removes the \n*/
      }
    
      while (fscanf(pf, "%s",str) == 1) /*continue to scan and print strings until the EOF*/
      {
        int v = strcmp (ustring, str);
    
        if (v == 0)
        {
          printf ("<%s> equals <%s>.\n", ustring, str);
        }
        printf("previous string is: %s \n", str);
        strcpy(prvstring, str);
        printf("new string is: %s\n", prvstring);
      }
      fclose(pf);  /*close file*/
      return 0;
    }
    Do you know why is it doing that?

    Thanks

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by xp5 View Post
    Do you know why is it doing that?
    Because prvstring is an uninitialized pointer. You either need to allocate memory for it to point to with malloc(), or declare it as a real array, not just a pointer.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    chpoint = strchr(ustring, '\n');  /*searches for "\n", address is returned if found; otherwise NULL is returned*/
    ustring is not initialized.

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    33
    Hi "ustring" is initialize as "char ustring[STRSIZE]". Am I supposed to do it differently?

    I modified the code into using array now, but I can't get it to work, it says : "hw1n2.c:38: warning: passing arg 1 of `strcpy' makes pointer from integer without a cast"

    I'm not sure on how to fix this problem, the following is my revised code:
    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main(void)
    {
      #define STRSIZE 20
      FILE *pf;
      char str[STRSIZE];  /*Maximum size of a string is 20*/
      char ustring[STRSIZE];
      char *chpoint;
      char prstring[STRSIZE];
    
      pf = fopen("input.txt","r");
    
      if (!pf)
      {
        printf("Can't open the file\n");
        return 0;
      }
    
      printf("Enter a string. ");
      chpoint = strchr(ustring, '\n');  /*searches for "\n", address is returned if found; otherwise NULL is returned*/
    
      if (chpoint != NULL)
      {
        *chpoint = 0;   /*removes the \n*/
      }
    
      while (fscanf(pf, "%s",str) == 1) /*continue to scan and print strings until the EOF*/
      {
        int v = strcmp (ustring, str);
    
        if (v == 0)
        {
          printf ("<%s> equals <%s>.\n", ustring, str);
        }
        printf("previous string is: %s \n", str);
        strcpy(prstring[STRSIZE], str);
        printf("new string is: %s\n", prstring[STRSIZE]);
      }
      fclose(pf);  /*close file*/
      return 0;
    }
    Thank you for your help!

  7. #7
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    he means that ustring does not actually contain a string.

    you probably want something like:

    Code:
    printf("Enter a string. ");
    scanf("&#37;s", ustring);
    chpoint = strchr(ustring, '\n');  /*searches for "\n", address is returned if found; otherwise NULL is returned*/

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by robwhit View Post
    ustring is not initialized.
    That doesn't matter. It's (presumably) big enough to hold the result. prvstring, on the other hand, is just an uninitialized pointer. If only people would indicate which LINE caused the seg fault...

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by brewbuck View Post
    That doesn't matter. It's (presumably) big enough to hold the result.
    it doesn't hold a result. If it doesn't have a null terminated element, then it will have an array overrun in strchr.

    to initialize the array do this:
    Code:
    char ustring[STRSIZE]={0};
    Last edited by robwhit; 08-29-2007 at 11:11 PM.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by robwhit View Post
    it doesn' t hold a result. If it doesn't have a null terminated element, then it will have an array overrun in strchr.
    Didn't look closely enough. Again, perhaps I should sleep!

  11. #11
    Registered User
    Join Date
    Aug 2007
    Posts
    33

    Thank you!!

    Hi,

    Thank you very much for your help!! I'm done with my homework thanks to you! My final code looks like this:

    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main(void)
    {
      #define STRSIZE 20
      FILE *pf;
      char str[STRSIZE];  /*Maximum size of a string is 20*/
      char ustring[STRSIZE]={0};
      char *chpoint;
      char prstring[STRSIZE];
    
      pf = fopen("input.txt","r");
    
      if (!pf)
      {
        printf("Can't open the file\n");
        return 0;
      }
    
      printf("Enter a string. ");
      fgets(ustring, STRSIZE, stdin);
      chpoint = strchr(ustring, '\n');  /*searches for "\n", address is returned if found; otherwise NULL is returned*/
    
      if (chpoint != NULL)
      {
        *chpoint = 0;   /*removes the \n*/
      }
    
      while (fscanf(pf, "&#37;s",str) == 1) /*continue to scan and print strings until the EOF*/
      {
        int v = strcmp (ustring, str);
    
        if (v == 0)
        {
          printf("the output is: %s %s\n", prstring, str);
        }
        strcpy(prstring, str);
      }
      fclose(pf);  /*close file*/
      return 0;
    }
    THANKS!!

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    you really ought to initialize prstring like brewbuck said, too.

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you should check the return value of fgets before calling strchr
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM