Thread: Struct and typedef

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    Struct and typedef

    Hi, I have this code and I wants by using struct to insert "1" to the size and afterwards print it, but it gives a "syntax ; error" although all ";" decent in my code's script.
    code:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include<stdlib.h>
    
    
    typedef struct
    {
    	char num[4];
    	char ters[4];
    	int  size;
    	char *address;
    	 
    
    
    }memIdentifier;
    
    
    main()
    {
    	memIdentifier.size=1;
            memIdentifier.addres='Hello';
    	printf("%d", memIdentifier.size);
            printf("%c", memIdentifier.address);
    	return 0;
    }
    any help for how to correct it for printing? thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The typedef means that memIdentifier is a type, not a variable name. You need to declare a variable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    The typedef means that memIdentifier is a type, not a variable name. You need to declare a variable.
    GOT you, but got wrong printing-it prints 1 but in the sl.address prints something weird!(not like input at all)
    Here's the code after edition:
    code:
    Code:
    typedef struct
    {
        char num[4];
        char ters[4];
        int  size;
        char *address;
          
     
     
    }memIdentifier;
     
     
    main()
    {
    	memIdentifier sl;
        sl.size=1;
    	sl.address="hello";
        printf("%d", sl.size);
    	printf("%c", sl.address);
        return 0;
    }
    Moreover, I have another question, when I write in the struct's structure itself a pointer like "char*address" why if I would call it I write the variable.address, and not variable.(*address) ?
    thanks

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Where have you allocated memory for that pointer in your structure?

    You really really need to start finding and reading the documentation for the standard functions before you try to use them. In this instance you need to find and read the documentation for printf() paying very close attention to the format specifiers. Much like scanf() you must insure that the format specifiers match the data types.

    Jim

  5. #5
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by jimblumberg View Post
    Where have you allocated memory for that pointer in your structure?

    You really really need to start finding and reading the documentation for the standard functions before you try to use them. In this instance you need to find and read the documentation for printf() paying very close attention to the format specifiers. Much like scanf() you must insure that the format specifiers match the data types.

    Jim
    You are always saying that and the same content, I already have read them and dont make from your self something you don't really know about! I'm sorry for saying that but from your question "where have you allocated...." sounds you are really still not skilled of, and just throwing silly tips arbitrary.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    address is pointer to string - so it should be printed using %s format, %c is for 1 character, not string
    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

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You always saying that and the same content, I already have read them and dont make from your self something you don't really know about!
    Then you need to re-read the documentation, since you still don't seem to understand how to use the function. Why are you trying to use the specifier for a single character for a value that you seem to think is a string?

    I'm sorry for saying that but from your question "where have you allocated...." sounds you are really still not skilled of, and just throwing silly tips arbitrary.
    Well then why can't you answer the question? The answer of course if you never allocated any memory for that pointer.

    And as I said in your last topic, I don't know what compiler you're using but you are either ignoring warnings and errors or your compiler is defective. Your code has several deficiencies that a decent compiler should be able to warn you about. Look at what my compiler says about your code.

    main.c|13|error: return type defaults to ‘int’|
    main.c||In function ‘main’:|
    main.c|18|error: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]|
    main.c|18|warning: incompatible implicit declaration of built-in function ‘printf’|
    main.c|19|warning: format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat=]|
    ||=== Build finished: 2 errors, 2 warnings (0 minutes, 0 seconds) ===|
    Also you should know by now that you can't use the assignment operator to assign values to strings.

    Jim

  8. #8
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by vart View Post
    address is pointer to string - so it should be printed using %s format, %c is for 1 character, not string
    Alright, but isn't *address mean like we go to the value that's found in pointer address? and that pointer isn't gonna be changed unless I ordered that.

  9. #9
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by jimblumberg View Post
    Then you need to re-read the documentation, since you still don't seem to understand how to use the function. Why are you trying to use the specifier for a single character for a value that you seem to think is a string?


    Well then why can't you answer the question? The answer of course if you never allocated any memory for that pointer.

    And as I said in your last topic, I don't know what compiler you're using but you are either ignoring warnings and errors or your compiler is defective. Your code has several deficiencies that a decent compiler should be able to warn you about. Look at what my compiler says about your code.



    Also you should know by now that you can't use the assignment operator to assign values to strings.

    Jim
    what compiler are you using? that's not appearing for me at all, if it would, then I wouldn't ask at all .

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I use gcc.

    What compiler/IDE do you use that doesn't generate any warnings for your code?

    Jim
    Last edited by jimblumberg; 06-07-2015 at 10:39 AM.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by RyanC
    Alright, but isn't *address mean like we go to the value that's found in pointer address? and that pointer isn't gonna be changed unless I ordered that.
    Yes, it is true that given a pointer p, *p results in what the pointer points to. In the code that you posted in post #3, sl.address is a char*, i.e., a pointer to char. Therefore, *sl.address is a char. But the idea here is that for printf, %c is used to print a char, whereas %s is used to print a null terminated string. To print a null terminated string, you need to pass a corresponding argument that is a pointer to the first element of the string. sl.address is precisely that.

    jimblumberg's concern in post #4 about "where have you allocated memory for that pointer in your structure?" is not a problem in your post #3 because you have assigned a string literal to sl.address. However, it is prophylactic in that chances are, you will want to do more than just assign string literals, in which case you need to start thinking of allocating memory, e.g., with malloc. If you do not actually need such dynamic memory allocation, then perhaps sl.address should be an array of char instead of a pointer to char.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. typedef struct from .h and .c
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-16-2008, 07:35 PM
  2. differences between struct and typedef struct
    By stanlvw in forum C Programming
    Replies: 1
    Last Post: 07-22-2008, 03:28 PM
  3. typedef struct
    By Gerread in forum C Programming
    Replies: 2
    Last Post: 12-12-2007, 05:20 AM
  4. Diff btw typedef struct and struct?
    By willkoh in forum C Programming
    Replies: 3
    Last Post: 09-24-2005, 02:27 PM
  5. typedef struct
    By ... in forum C++ Programming
    Replies: 5
    Last Post: 01-19-2004, 03:17 PM