Hello,

I am trying to get the following code to print out the number of days in each month using pointers.
This what the program currently prints:

Enter a year (ex. 1997): 2001
Year entered: 2001

--Not a Leap Year!--

Number of days in each month:
January: 0
February: 0
March: 0
April: 0
May: 0
June: 0
July: 0
August: 0
September: 0
October: 0
November: 0
December: 0

I am still trying to get my head around pointers so any help would be really appreciated. Thank you

Code:

#include <stdlib.h>
#include <stdio.h>

static int get_days_in_month(int **days, int year) ;

int main(int argc, char **argv) {
	
	int nFields ;
	int year = 0 ;
	int days[12] ;
	char *months[12] = {"January", "February", "March", "April", "May", 
		"June", "July", "August", "September", "October", "November", 
		"December"} ;
	
	
	/* Get user year */
	printf("Enter a year (ex. 1997): " ) ;
	nFields = scanf("%d", &year) ;
	
	/* Verify letters were not entered */
	if (nFields == 0) {
		printf("Incorrect Input\n") ;
		return 0 ;
	}
	
	/* Call function */
	days[12] = get_days_in_month(days, year) ;
	
	/* Print months with # of days */
	printf("Number of days in each month: \n") ;
	for (int i = 0 ; i < 12 ; i++) {
		
		printf("%10s: %2d\n", months[i], days[i]) ;
		
	}
	
    return EXIT_SUCCESS;
}

static int get_days_in_month(int **days, int year) {
	
	days = malloc(sizeof(int) * 12) ;
	
	/* number of days in each month excluding February */
	days[0] = 31 ;
	days[2] = 31 ;
	days[3] = 30 ;
	days[4] = 31 ;
	days[5] = 30 ;
	days[6] = 31 ;
	days[7] = 31 ;
	days[8] = 30 ;
	days[9] = 31 ;
	days[10] = 30 ;
	days[11] = 31 ;
	
	printf("Year entered: %d\n", year) ;
	
	/* Leap Year found */
	if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
		printf("\n--Leap Year!!--\n\n") ;
		
		days[1] = 29 ;
		
	} else { /* Not a Leap Year */
		
		printf("\n--Not a Leap Year!--\n\n") ;
		
		days[1] = 28 ;
		
	}
	
	return days ;
	
}

I tried to simplify your code and here's what I arrived at...I got your function working now. Please note, you don't have to allocate the memory for an array that already exists.
You should be able to take this and complete the rest.

#include <stdlib.h>
#include <stdio.h>

void get_days_in_month(int days[], int year);

int main(int argc, char **argv) {
	
	int nFields, i;
	int year = 0 ;
	int days[12] ;
	char *months[12] = {"January", "February", "March", "April", "May", 
		"June", "July", "August", "September", "October", "November", 
		"December"} ;
	
	
	/* Get user year */
	printf("Enter a year (ex. 1997): " ) ;
	nFields = scanf("%d", &year) ;
	
	/* Verify letters were not entered */
	if (nFields == 0) {
		printf("Incorrect Input\n") ;
		return 0 ;
	}
	
	/* Call function */
	get_days_in_month(days, year) ;
	
	/* Print months with # of days */
	printf("Number of days in each month: \n") ;
	for (i = 0 ; i < 12 ; i++) 
	{
		
		printf("%10s: %2d\n", months[i], days[i]) ;
		
	}
	
    return EXIT_SUCCESS;
}

void get_days_in_month(int days[], int year) 
{
	days[0] = 31 ;
	days[2] = 31 ;
	days[3] = 30 ;
	days[4] = 31 ;
	days[5] = 30 ;
	days[6] = 31 ;
	days[7] = 31 ;
	days[8] = 30 ;
	days[9] = 31 ;
	days[10] = 30 ;
	days[11] = 31 ;
}

Edited 6 Years Ago by gerard4143: n/a

Your get_days_in_month() has a major problem.
You malloc() the day array but never free it, creating a memory leak. You'd be better off just creating the array as a pointer in the main() function.

Hello,
I am still unable to get this functioning. As part of our assignment, we have to use:

static int get_days_in_month(int **days, int year) ;

Unfortunately we can't pass it through an array.

To help clear things up, here are the instructions:
For this assignment you will modify the function get_days_in_month in the previous assignment to dynamically allocate an array of 12 integers and return it to the caller via a parameter.
The new function get_days_in_month will use malloc or calloc to allocate an array of 12 integers. It will then fill the array as in the previous assignment, and return the array to the caller via the parameter days; when the array is no longer needed the caller should free it.

I really am not trying to get anyone to do my homework for me, I just don't understand what I am doing wrong.

I took out a lot of the code unneeded code because I am just trying to get the correct values to print. Hopefully from there I can get the rest of it to work.

#include <stdlib.h>
#include <stdio.h>

static int get_days_in_month(int **days, int year) ;

int main(int argc, char **argv) {
	
	int nFields ;
	int year = 0 ;
	int *days_In_Month = NULL ;	
	
	/* Get user year */
	printf("Enter a year (ex. 1997): " ) ;
	nFields = scanf("%d", &year) ;
	
	get_days_in_month(&days_In_Month, year) ;
		
    return EXIT_SUCCESS;
}

static int get_days_in_month(int **days, int year) {
	
	int *array = malloc(sizeof(int) * 12) ;
	*days = array ;

	/* number of days in each month excluding February */
	array[0] = 31 ;
	array[1] = 28 ;
	array[2] = 31 ;
	array[3] = 30 ;
	array[4] = 31 ;/*
	*days[5] = 30 ;
	*days[6] = 31 ;
	*days[7] = 31 ;
	*days[8] = 30 ;
	*days[9] = 31 ;
	*days[10] = 30 ;
	*days[11] = 31 ;*/
	//*days = array ;

	for (int i = 0 ; i < 5; i++) {
		printf("Days %d: %d\n", i, **days++); //“EXC_BAD_ACCESS” error message
	}
	
	/*printf("Days[0]: %d\n", **days++);
	printf("Days[2]: %d\n", **days);
	printf("Days[3]: %d\n", *days[3]);*/
	
	free(array) ;
	
	return days ;
}

Thanks for any help!

Hello,

I modified it again to return the address, but it will only print the first number in the array correctly:

Enter a year (ex. 1997): 2001
days in month 31
days in month 2001
days in month 1
days in month 1606416160
days in month 32767

Any suggestions would be greatly appreciated.

#include <stdlib.h>
#include <stdio.h>

static int get_days_in_month(int **days, int year) ;

int main(int argc, char **argv) {
	
	int nFields ;
	int year = 0 ;
	int *days_In_Month = NULL ;	
	
	/* Get user year */
	printf("Enter a year (ex. 1997): " ) ;
	nFields = scanf("%d", &year) ;
	
	int getAddress ;
	
	getAddress = get_days_in_month(&days_In_Month, year) ;
	
	days_In_Month = &getAddress ;
	
	printf("days in month %d\n", *days_In_Month);
	printf("days in month %d\n", *(days_In_Month + 1));
	printf("days in month %d\n", *(days_In_Month + 2));
	printf("days in month %d\n", *(days_In_Month + 3));
	printf("days in month %d\n", *(days_In_Month + 4));

    return EXIT_SUCCESS;
}

static int get_days_in_month(int **days, int year) {
	
	int *array = malloc(sizeof(int) * 5) ;
	days = &array ;

	/* number of days in each month excluding February */
	array[0] = 31 ;
	array[1] = 28 ;
	array[2] = 31 ;
	array[3] = 30 ;
	array[4] = 31 ;
	
	free(array) ;
	
	return **days ;
	
}

Edited 6 Years Ago by DCvonB: n/a

Again this is a stripped down verion of what your trying to accomplish

#include <stdlib.h>
#include <stdio.h>

void get_days_in_month(int **days);

int main(int argc, char **argv) 
{
	
	int *days_In_Month = NULL ;	
	
	
	get_days_in_month(&days_In_Month) ;
	
	printf("days in month %d\n", *(days_In_Month + 1));
	printf("days in month %d\n", *(days_In_Month + 2));
	printf("days in month %d\n", *(days_In_Month + 3));
	printf("days in month %d\n", *(days_In_Month + 4));

	free(days_In_Month);

	return EXIT_SUCCESS;
}

void get_days_in_month(int **days) 
{
	
	*days = malloc(sizeof(int) * 5) ;

	/* number of days in each month excluding February */
	(*days)[0] = 31 ;
	(*days)[1] = 28 ;
	(*days)[2] = 31 ;
	(*days)[3] = 30 ;
	(*days)[4] = 31 ;
	
}

Edited 6 Years Ago by gerard4143: n/a

I reallu don't understand this function at all:

static int get_days_in_month(int **days, int year) {
	
	int *array = malloc(sizeof(int) * 5) ;
	days = &array ;

	/* number of days in each month excluding February */
	array[0] = 31 ;
	array[1] = 28 ;
	array[2] = 31 ;
	array[3] = 30 ;
	array[4] = 31 ;
	
	free(array) ;
	
	return **days ;
}

1) Why are you malloc ing and array just to fill it with data, then destroy it with free() ? What's wrong with: int array[] = {31, 28, 31...}; It's always there, ready to do, without the hits on the heap.

2) Why are you passing in a ** and returning a **? If you put the above definition in main() you don't need this worthless function.

3) It will be less confusing and you would not be struggling with this code 2 days later.

4) This function could then be used to calculate the number of days which is where I think you are eventually going.

Again this is a stripped down verion of what your trying to accomplish

and here's my stripped down version:

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char **argv) 
{
	
    int *days_In_Month = { 0, 31,28,31,30,31,30, 
                              31,31,30,31,30,31} ;	
	
	printf("days in month %d\n", *(days_In_Month + 1));
	printf("days in month %d\n", *(days_In_Month + 2));
	printf("days in month %d\n", *(days_In_Month + 3));
	printf("days in month %d\n", *(days_In_Month + 4));

	return EXIT_SUCCESS;
}

well, here's my stripped down version :)

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char **argv) 
{
    int i, days_In_Month[12] = { 31,28,31,30,31,30,31,31,30,31,30,31} ;	
    char *month[12] = {"January",   "February", "March",    "April", 
                       "May",       "June",     "July",     "August", 
                       "September", "October",  "November", "December"} ;	
	
    for (i = 0; i < 12; i++)
        printf("%s has %d days.\n", month[i], days_In_Month[i]);

    return EXIT_SUCCESS;
}

Edited 6 Years Ago by jephthah: n/a

The assignment says to allocate in the function, and free() it in main.

void get_days_in_month(int **days, int year) {
 	int *array = malloc(sizeof(int) * 5) ;
	/* Set the output variable */
	*days = array ; 
	/* do not increment either pointer (days or array) */
	array[0] = 31 ;
	array[1] = 28 ;
	array[2] = 31 ;
	array[3] = 30 ;
	array[4] = 31 ; 
	/* Do not free() here, the caller will do it */
	/* No need to return the pointer more than once */
	return;	
}

Note days_In_Month[2] is alternate (more readable) syntax
which means the same as *(days_In_Month + 2)

int main(int argc, char **argv)
{
	int *days_In_Month;
	...
	get_days_in_month(&days_In_Month, year) ;

	for (month = 0; month <= 5; month++) {
	 	printf("days in month %d = %d\n",
			month, days_In_Month[month]);
	}

	free(days_In_Month); /* Free() after using */

   return EXIT_SUCCESS;
}

Edited 6 Years Ago by UncleLeroy: Add assignment note.

The assignment says to allocate in the function, and free() it in main.

void get_days_in_month(int **days, int year) {
 	int *array = malloc(sizeof(int) * 5) ;
	/* Set the output variable */
	*days = array ; 
	/* do not increment either pointer (days or array) */
	array[0] = 31 ;
	array[1] = 28 ;
	array[2] = 31 ;
	array[3] = 30 ;
	array[4] = 31 ; 
	/* Do not free() here, the caller will do it */
	/* No need to return the pointer more than once */
	return;	
}

Note days_In_Month[2] is alternate (more readable) syntax
which means the same as *(days_In_Month + 2)

int main(int argc, char **argv)
{
	int *days_In_Month;
	...
	get_days_in_month(&days_In_Month, year) ;

	for (month = 0; month <= 5; month++) {
	 	printf("days in month %d = %d\n",
			month, days_In_Month[month]);
	}

	free(days_In_Month); /* Free() after using */

   return EXIT_SUCCESS;
}

Where is the obligatory check for the return of malloc() before making use of it, or in the same token, before free()'ing the memory in main()?
Playing with segmentation faults is not the way to go.

Comments
If you want to play this bad rep game, I'm for it. I have a bigger stick then you, so your rep will go to negative long before mine.
This article has been dead for over six months. Start a new discussion instead.