memoops.c

/*
 * demonstrate how sizeof readts to a pointer constant and a pointer variable
 *
 * Matt Bishop, ECS 36A
 * * May 14, 2024: original version
 */
#include <stdio.h>
#include <malloc.h>

/* the variables */
char a[100];		/* array of 100 chars -- a is pointer constant */
char *b;		/* pointer variable */

int main(void)
{
	/*
	 * first the array (pointer constant)
	 */
	printf("First, the pointer constant . . .\n");
	printf("sizeof(a) = %lu\n", sizeof(a));
	printf("number of bytes in array a = %lu\n\n", sizeof(a[0]) * 100);

	/*
	 * allocate space for b
	 */
	if ((b = malloc(100 * sizeof(char))) == NULL){
		perror("malloc");
		return(1);
	}
	
	/*
	 * next the allocated array (pointer variable)
	 */
	printf("Next, the pointer variable . . .\n");
	printf("sizeof(b) = %lu\n", sizeof(b));
	printf("number of bytes in array b = %lu\n", sizeof(b[0]) * 100);

	/* phew -- glad that's over */
	return(0);
}


UC Davis sigil
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email: mabishop@ucdavis.edu
ECS 36A, Programming & Problem Solving
Version of April 2, 2024 at 12:13PM

You can get the raw source code here.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh