What's wrong with this code?

This is a discussion on What's wrong with this code? within the C Programming forums, part of the General Programming Boards category; Code: /* #include<stdio.h> struct msg{ char *p1; char *p2; char *p3; }mymsg; mymsg.p1="Teach yourself C in 21 days!"; mymsg.p2="By SAMS ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    18

    What's wrong with this code?

    Code:
    /*
    #include<stdio.h>
    
    struct msg{
      char *p1;
      char *p2;
      char *p3;
    }mymsg;
    
    mymsg.p1="Teach yourself C in 21 days!";
    mymsg.p2="By SAMS publishing.";
    
    int main(void)
    {
      printf("mymsg.p1 points to %s\n",mymsg.p1);
      printf("mymsg.p2 points to %s\n",mymsg.p2);
    
      /* p3 points to the same string p1 points to */
      mymsg.p3=mymsg.p1;
      printf("mymsg.p3 points to the string p1 points to %s\n",mymsg.p3);
    
      /* p3 points to the same string p2 points to */
      mymsg.p3=mymsg.p2;
      printf("mymsg.p3 points to the string p2 points to  %s\n",mymsg.p3);
    
      return 0;
    }
    */
    My compiler tells me:

    "stru_array3.c", line 9: warning: old-style declaration or incorrect type for: mymsg
    "stru_array3.c", line 9: identifier redeclared: mymsg
    current : int
    previous: struct msg {pointer to char p1, pointer to char p1, pointer to char p2, pointer to char p3} : "stru_array3.c", line 7

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You can't make assignments to the structure members in global scope. Initialization is allowed though:
    Code:
    #include<stdio.h>
    
    struct msg{
        char *p1;
        char *p2;
        char *p3;
    }mymsg = {
        "Teach yourself C in 21 days!",
        "By SAMS publishing."
    };
    
    int main(void)
    {
        printf("mymsg.p1 points to %s\n",mymsg.p1);
        printf("mymsg.p2 points to %s\n",mymsg.p2);
        
        /* p3 points to the same string p1 points to */
        mymsg.p3=mymsg.p1;
        printf("mymsg.p3 points to the string p1 points to %s\n",mymsg.p3);
        
        /* p3 points to the same string p2 points to */
        mymsg.p3=mymsg.p2;
        printf("mymsg.p3 points to the string p2 points to  %s\n",mymsg.p3);
        
        return 0;
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    8

    Re: What's wrong with this code?

    m not sure but dont you think it would be better if you declared your structure in the main function ??

    Secondly

    char *p1 is only one byte of memory space. This can store just one alphabet! therefore i dont think that it is possible to have a whole string like "teach your self.....21 days"



    Originally posted by ylzhang
    Code:
    /*
    #include<stdio.h>
    
    struct msg{
      char *p1;
      char *p2;
      char *p3;
    }mymsg;
    
    mymsg.p1="Teach yourself C in 21 days!";
    mymsg.p2="By SAMS publishing.";
    
    int main(void)
    {
      printf("mymsg.p1 points to %s\n",mymsg.p1);
      printf("mymsg.p2 points to %s\n",mymsg.p2);
    
      /* p3 points to the same string p1 points to */
      mymsg.p3=mymsg.p1;
      printf("mymsg.p3 points to the string p1 points to %s\n",mymsg.p3);
    
      /* p3 points to the same string p2 points to */
      mymsg.p3=mymsg.p2;
      printf("mymsg.p3 points to the string p2 points to  %s\n",mymsg.p3);
    
      return 0;
    }
    */
    My compiler tells me:

    "stru_array3.c", line 9: warning: old-style declaration or incorrect type for: mymsg
    "stru_array3.c", line 9: identifier redeclared: mymsg
    current : int
    previous: struct msg {pointer to char p1, pointer to char p1, pointer to char p2, pointer to char p3} : "stru_array3.c", line 7

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >dont you think it would be better if you declared your structure in the main function ??
    It depends on the programmer, but 97% (made up number) of structures are used in multiple functions, so declaring it to be local to main wouldn't be the best of ideas in most cases.

    >char *p1 is only one byte of memory space.
    Actually, a pointer is more likely to be two or four bytes of memory, but this size can vary depending on the machine. A char is one byte, a pointer to char is completely different.

    >This can store just one alphabet!
    It can store the address of a single location in memory that happens to contain just one character.

    >therefore i dont think that it is possible to have a whole string like "teach your self.....21 days"
    Of course it can, a string literal is a pointer to the first character in a sequence of contiguous characters. If you have the first address, it's a simple matter of pointer arithmetic:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char *p = "A sequence of characters\n";
    
        while (*p)
            putchar(*p++);
    
        return 0;
    }
    Of course, the standard library saves us from actually doing this in a serious program:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char *p = "A sequence of characters\n";
    
        fputs(p, stdout);
    
        return 0;
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 08:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 01:51 AM

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