9511_workbook

solved exercises from algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
guia04/ex08.c (1320B)
   1 /*    G04E08 - Different declarations of arrays and pointers
   2  *    by Martin J. Kloeckner    
   3  *    github.com/klewer-martin    */
   4 
   5 #include <stdio.h>
   6 #include <string.h>
   7 
   8 int main (void)
   9 {
  10     char cadena1[] = "Hola"; 
  11     char *cadena2 = "Hola"; 
  12     /* The difference is that cadena1 is an array that contains the secuence of chars "Hola", and cadena2 is a pointer which points to an array of chars that contains "Hola"; */
  13 
  14     char meses1 [12][] = {"Enero", "Febrero", ... , "Diciembre"}; 
  15     char * meses2 [12] = {"Enero", "Febrero", ... , "Diciembre"}; 
  16     /* Similar as the previous example, meses1 is an array of arrays of chars, and meses2 is a pointer to an array of chars which the maximum length is 12; */
  17 
  18     /* The first of this statements doesn't work because cadena1 is an array so the adress can't be modified; cadena2 instead is a pointer, so we are just pointing cadena2 to "Chau". */
  19     cadena1 = "Chau";
  20     cadena2 = "Chau"; 
  21     
  22     /* The first statement works because, it's how strcpy is intended to be used, the third statement its wrong because it's trying to copy more characters than cadena1 it's meant to hold, and the second statement won't work because strcpy treats the first argument as an array, and cadena2 ain't an array  */ 
  23     strcpy(cadena1,"Chau"); 
  24     strcpy(cadena2,"Chau"); 
  25     strcpy(cadena1,"Hola y chau"); 
  26 
  27     return 0;
  28 }