sk01
sk01

Reputation: 55

Passing strings from functions

I am new to coding and at my university we are learning C and C++ currently.

I have a problem with my C code. I want the code to say which day of the week it is when I enter a date. I get multiple errors with CodeBlocks.

I have to make an extra function for the "zeller's congruency".

I am not very sure how to get a string or a character from an other function.

I get the warning: "warning: assignment makes integer from pointer without a cast" at all cases of the switch. And in my main function I get the warning: "warning: format '%s' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat=]"

header.h

int ta, mo, ja, h ,q ,m ,k , j;
char zk(int, int, int) ;

main.c

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

int main()
{
int tt, mm, jj;
printf("Geben Sie das Datum ein: ");
scanf("%i %i %i", &tt, &mm, &jj);
printf("\n");
char *p = zk(tt, mm, jj);
printf("%s", *p);
return 0;
}

zellerskongruenz.c

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

char zk(int ta, int mo, int ja)
{
if (mo == 1){mo = 13; ja--; }
if (mo == 2){mo = 14; ja--; }
q = mo;
k = ja % 100;
j = ja / 100;
h = ta + ((q+1)*13)/5 + k + k/4 + j/4 - 2*j;
h = h % 7;

char *wochentag;

switch(h)
{
case 0 : *wochentag = "Samstag"; break;
case 1 : *wochentag = "Sonntag"; break;
case 2 : *wochentag = "Montag"; break;
case 3 : *wochentag = "Dienstag"; break;
case 4 : *wochentag = "Mittwoch"; break;
case 5 : *wochentag = "Donnerstag"; break;
case 6 : *wochentag = "Freitag"; break;
} 

return wochentag;
}

I am very thankful for anyone who tries to help me.

Upvotes: 2

Views: 84

Answers (3)

Daniel Goldfarb
Daniel Goldfarb

Reputation: 7744

The string (character array) must be passed back in one of the function parameters. C convention is to put output arguments on the left, input on the right (to mimic the right-to-left "movement" of an assignment statement. Thus:

void zk(char *wochentag, int ta, int mo, int ja)

Also, in main, you should declare a character array, with the maximum number of characters that you might get back.

int main()
{
    int tt, mm, jj;
    printf("Geben Sie das Datum ein: ");
    scanf("%i %i %i", &tt, &mm, &jj);
    printf("\n");
    char wochentag[64];  /* 64 is an arbitrary maximum */
    zk(wochentag, tt, mm, jj);
    printf("%s", wochentag);
    return 0;
}

The suggestion by another answer to make the function pass back a character pointer MAY work in this case because the pointer will point to a literal which should be in the global segment of the program and thus not go out of scope. However this is not guaranteed and is very dangerous. To be sure, if you want to pass back a pointer, best practice is that the strings to which the pointer points, should ideally be declared static and be placed globally (i.e. outside of any function). Better, and more common convention, is to have the caller pass in a char array and the function fills it for the caller.

Upvotes: 0

ameyCU
ameyCU

Reputation: 16607

printf("%s", *p);

This is reason for error in main. *p is of type char not char * , so use %c to print single character. Use %s if you want to print complete string stored in p.

Second warning is about these -

case 0 : *wochentag = "Samstag"; break;
         ^^^^^^^^^^^^^^^^^^^^^^

you can't do this way even type doesn't match.

You should allocate memory to wochentag and then use strcpy to copy string.

Upvotes: 1

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53036

Your zk() is declared to return char and you are trying to assign the result to char *.

Also, because you are returning string literals it's good practice to declare the function as const char * instead to at least avoid accidentally attempting to modify the string which would be illegal.

And see @ameyCU's answer too.

Upvotes: 1

Related Questions