Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am not used to C programming so i would like to understand the useage of * operator in C pointers.I tried to google it but nothing is quite convincing.(An example will be very good)
I would like to know What the following indicate or function as:-
1. int *i;
2. Struct My_DataBase *My_Name; (My_DataBase is defined earlier as a struct)
3. int* i;
4. j = *(int *)k; (j and k are defined as integers).
5. Struct My_DataBase *My_Name1 = (My_DataBase *) (j + (k * l));

Kindly help and let me know if you have any
Posted

(1) declares i as a pointer to an int.
(2) declares My_Name as a pointer to a My_Database struct.
(3) has the same meaning of (1).
(4) interpreting k as pointer to int (i.e. assuming that k is an address) set j to the int value pointed.
(i.e. get the int value having address k).
(5) interpreting the expression (j+(k*l)) as a pointer to a struct My_DataBase, set My_Name1 pointing to the same location.

I guess (2),(5) should be written:
2. struct My_DataBase *My_Name;
5. struct My_DataBase *My_Name1 = (struct My_DataBase *) (j + (k * l));

:)
 
Share this answer
 
v2
Be careful about the little differences between C and C++:


  1. in C++ is admitted to write int* p; and int *p; to declare p as a pointer to integer. In C you should write int *p;
  2. in C to declare a variable of a struct type you should always use the following syntax:
    C
    struct MyStruct
    {
       ...
    };
    
    struct MyStruct a;
    struct MyStruct *b;

    In C++ you can omit the struct keyword when declaring variables:
    C++
    struct MyStruct
    {
       ...
    };
    
    MyStruct a;
    MyStruct *b;
 
Share this answer
 
v2
Comments
CPallini 4-Nov-10 7:42am    
Are you sure about C pointer declaration? I guess most C compilers would accept 'int* p' (at least Microsoft CL and GCC do).
Sauro Viti 4-Nov-10 9:13am    
You make me doubt! It 's been so long since I studied books on C!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900