Tutorial de Lenguaje C                                                                                               Dr. Roberto Gómez


LOS APUNTADORES


Declaración de apuntadores
 

                                                    <tipo> * <variable>                                                     int *contPtr, cont;
                                                    float *res;
                                                    unsugned int *nosigno;
                                                    char *mensaje;


Los operadores de los apuntadores

1. Un operador de dirección &


prog33.c Primer ejemplo apuntadores


                            #include <stdio.h>
                            int main()

                           {
                                            int y;
                                            int *yPtr;

                                            y = 5;
                                            yPtr = &y;
                                            printf("Valor de y: %d \n",y);
                                            printf("Valor de yPtr: %d \n",yPtr);
                                           
                            }

2. Un operador de indirección o de desreferencia


prog34.c Segundo ejemplo apuntadores

                           #include <stdio.h>
                            main( )
                            {

                                            int x,y;
                                            int *py;

                                            y = 5;
                                            *py = y;
                                            x = *py + 5;
                                            printf(''Valor de x: %d y de y: %d   \n'',*py,x);
                            }
 

Apuntadores y argumentos funciones
 

                        void permuta(x, y)
                                        int x,y;
                        {
                                        int temp;

                                        temp =x;
                                        x = y;

                                        y = x;
                        }

                      void permuta(px, py)

                                        int *px,*py;
                      {
                                        int temp;
                                        temp = *px;
                                        *px = *py;
                                        *py = *px;
                      }  

                        int main( )
                       {
                              int n;

                              scanf(''%d'',n);
                              printf(''%d'',n);
                        }


 

                        #include <stdio.h>
                        int
main( )

                        {
                              int *pn;

                              scanf(''%d'',pn);
                              printf(''%d'',*pn);
                         }


                       #include <stdio.h>
                       int
main( )

                       {
                             int n;
                             int *pn = &n;

                             scanf(''%d'',pn);
                             printf(''%d'',n);
                        }

 

                        #include <stdio.h>
                        int main( )
                        {
                                 int n;

                                 scanf(''%d'',&n);
                                 printf(''%d'',n);
                         }


 

MENÚ PRINCIPAL