Demo entry 1487297

homework

   

Submitted by anonymous on Apr 21, 2015 at 23:50
Language: C. Code size: 2.1 kB.

// =====================================================================================
//  
//       Filename:  arrays_and_pointers.c
//  
//    Description:  
//  
//        Version:  1.0
//        Created:  15.04.2015
//       Revision:  none
//       Compiler:  gcc
//  
//         Author:  Wolfgang Mehner (WM), mehner@vision.rwth-aachen.de
//   Organization:  
//  
// =====================================================================================

// #####   HEADER FILE INCLUDES   ###################################################

// C
#include <stdio.h>
#include <string.h>

// #####   MACROS  -  LOCAL TO THIS SOURCE FILE   ###################################

#define SHORT_ARRAY_LEN 10
#define LONG_ARRAY_LEN   8

// #####   TYPE DEFINITIONS  -  LOCAL TO THIS SOURCE FILE   #########################

// #####   DATA TYPES  -  LOCAL TO THIS SOURCE FILE   ###############################

// #####   VARIABLES  -  LOCAL TO THIS SOURCE FILE   ################################

// #####   PROTOTYPES  -  LOCAL TO THIS SOURCE FILE   ###############################

// #####   FUNCTION DEFINITIONS  -  EXPORTED FUNCTIONS   ############################

int main ( int argc, char *argv[] ) {
  // 1. declarations
  char		str[] = "Hyper, hyper!";
  short		array_short [ SHORT_ARRAY_LEN ] = { 0 };
  long		array_long  [ LONG_ARRAY_LEN  ] = { 3, 5, 7, 9, 2, 4, 6, 8 };
  int			i;

  char *p_str;
  long *p_long;

  printf ( "\n" );

  // 2. array of chars
  p_str = str;
  for ( i = 0; i < strlen(str); i += 1 ) {
    printf ( "%2d (%p) : %c\n", i, p_str, *p_str );
    p_str++;
  }
  printf ( "\n" );

  // 3. array of shorts
  for ( i = 0; i < SHORT_ARRAY_LEN; i += 1 ) {
    printf ( "%2d (%p) : %d\n", i, &( array_short[i] ), array_short[i] );
  }
  printf ( "\n" );

  // 4. array of longs
  p_long = array_long;
  for ( i = 0; i < LONG_ARRAY_LEN; i += 1 ) {
    printf ( "%2d (%p) : %ld\n", i, p_long, *p_long );
    p_long = p_long + 1;
  }
  printf ( "\n" );

  return 0;
}   // -----  end of function main  -----

// #####   FUNCTION DEFINITIONS  -  LOCAL TO THIS SOURCE FILE   #####################

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).