Page 2 of 2 First 12
  • Jump to page:
    #16
  1. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    Originally posted by kubicon
    After going over and over your "pointer to pointer" example a couple of posting up, I think I'm beggining to understand them.
    thanks oh wise1.

    Regarding this last posting. This
    char *str = "hello";
    is creating a pointer to the beginning of a literal string, right?
    It creates a pointer to the first character in the string. I know that's what you meant too, but it's useful to have the word character in there as we'll see in a minute.


    so if I try do do this:
    *str = "hello again";
    Basically what I'm telling the compiler is "Hey bob, overwrite the literal string with this one" in other words "Set the value of what str is pointing to this >hello again", and the compiler says, "but is a literal, I can't do that". I'm I right so far?
    Partly. There are two problems here. The first is that you're trying to overwrite a constant that is stored in memory that may be read only. The second is that "hello again" is converted to a pointer to a character, whereas *str is a character, not a pointer to a character.

    If we try to do this:

    char *str = "Hello World";
    *str = 'Y';

    We are now assigning a character rather than a pointer to a character but there is still a problem because we're trying to modify a string constant.

    If we try this:

    char myArray[] = "Hello World";
    char *str = myArray;
    *str = "Boo";

    Now we don't have a string constant but we're trying to assign a pointer to char where a char is expected.

    The following is fine:

    char myArray[] = "Hello World";
    char *str = myArray;
    *str = 'Y';

    You may recall I emphasised earlier that str is a pointer to the first _character_ in the string, and this is why.

    However if I do this
    str = "Hello again";
    I'm just redirecting the str pointer to point to the new literal string. The old literal string will still be there, I just won't be able to access it.
    That's correct. Or rather, it now points to the first character of the new string.
  2. #17
  3. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    12
    How would I init a structure like this, the book has this example:
    Code:
    1:  struct customer {
    2:      char firm[20];
    3:      char contact[25];
    4:  };
    5:
    6:  struct sale {
    7:      struct customer buyer;
    8:      char item[20];
    9:      float amount;
    10: };
    11:
    12:
    13: struct sale y1990[100] = {
    14:      { { "Acme Industries", "George Adams"},
    15:          "Left-handed widget",
    16:           1000.00
    17:      }
    18:      { { "Wilson & Co.", "Ed Wilson"},
    19:          "Type 12 gizmo",
    20:           290.00
    21:      }
    22: };
    but I am tryning it like this and it is not working (error about )'s):
    Code:
    typedef struct _k
    {
    	int x; 
    	int y; 
    } k; 
    
    typedef struct _p
    {
    	k boon; 
    	int z; 
    } p; 
    
    int main() 
    { 
    	p list[5] = 
    	{ 
    		{{500,100},300} 
    		{{1016,300},224} 
    		{{333,444},341} 
    	};
    	printf("%d %d %d\n",list[0].boon.x,list[0].boon.y,list[0].z); 
    	printf("%d %d %d\n",list[1].boon.x,list[1].boon.y,list[1].z); 
    	printf("%d %d %d\n",list[2].boon.x,list[2].boon.y,list[2].z); 
    return 0; 
    }
  4. #18
  5. pogremar
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Location
    At Work
    Posts
    958
    Rep Power
    13
    I better learn how to formulate my sentences correctly. Thanks for the clarification.
  6. #19
  7. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    12
    I am playing around with unions/structures, what is wrong with this:

    Code:
    typedef struct _gg 
    {
    	char type; 
    	typedef union _tt
    	{
    		int i; 
    		char c; 
    		float f; 
    	} tt; 
    }gg; 
    
    I want to have a union inside a structure, I get 'illegal storage class on line }tt; ...
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo