2

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.

3
  • Change char zk(... to const char * zk(..., char *wochentag; to const char *wochentag;, all *wochentag to be wochentag and printf("%s", *p); to be printf("%s", p);. Commented Dec 17, 2016 at 7:57
  • And, well, it should better be int main(void) in C. Commented Dec 17, 2016 at 7:58
  • Also the int ta, mo, ja defined in the header are useless, as they appear in zk()'s parameter list and with this are hidden. And the remaining h ,q ,m ,k , j; should better be defined locally to zk(), at least from the code you show there is no need to define them globally. Commented Dec 17, 2016 at 8:01

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your help. Now I could fix my code :)
1
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.

3 Comments

Wow ... I didn't see the *, because all of it in a single line seems confusing.
@iharob It is somewhat :)
Thank you for your help. Now I could fix my code :)
0

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.

3 Comments

Thank you for your help. Now I could fix my code :)
"The string (character array) must be passed back ...." returning a char* is perfectly fine.
The essential "must" here is to define the interfacing pointer to point to const, as it refers to a string literal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.