1

I have a problem with this piece of code here. What my code is supposed to do is to assign bitfields to weekdays. For example, 0x00 for Monday, 0x01 for Tuesday and so on. Here is my code:

    #include<stdio.h>

typedef struct
{
  unsigned int week:3;
  unsigned int month:4;
}datum;

void date(datum *d, char wday[])
{
  switch(d->week)
    {
    case 0x00:
     *wday = "Monday";
      break;
    case 0x01:
     *wday = "Tuesday";
      break;
    default:
      printf("Unknown option: %i\n", d->week);
    }
}

int main()
{
  char wday[]="";
  datum now = {0x01, 0x05};
  date(&now,&wday);
  printf("It's %s\n", wday);
  return 0;
}

At this point, what my code is SUPPOSED to do when I run it (and if I could at least compile it), is to show:

It's Monday

Unfortunately it does either not compile or when I make changes with the pointers it shows for example "It's -88". I think the problem lies with the char pointers to char wday. But I have no idea how to make my code work.

Thank you very much for helping out a C-Rookie.

  • You need more study-time with the C language and pointers. – WhozCraig Jun 30 '14 at 00:53
  • look at how you are defining wday. Where does it get allocated? That should get you on your way to compiling successfully. – bentank Jun 30 '14 at 00:56
  • 1
    You need to take a look at [**Pointers**](http://pw1.netcom.com/~tjensen/ptr/pointers.htm). It is an oldie, but a very thorough goodie. (there is a .pdf as well) – David C. Rankin Jun 30 '14 at 02:40

3 Answers3

1

I see the following problems in your code.

  1. The lines

     *wday = "Monday";
    

    and

     *wday = "Tuesday";
    

    are not legal. This is not how you assign strings in C. You will need to use:

    strcpy(wday, "Monday");
    

    and

    strcpy(wday, "Tuesday");
    

    Make sure you add

    #include <string.h>
    

    to use strcpy.

  2. You have the line:

    char wday[]="";
    

    and you are using it in the call to date. There isn't enough memory in wday to hold Monday or Tuesday. You need to declare it with large enough size to hold the dates. Changing it to something like:

    char wday[50];
    

    should work.

  3. You have the line:

    date(&now,&wday);
    

    What you need is

    date(&now, wday);
    

    When you use wday as an argument to a function call, it decays to type char*. If you use &wday, it becomes char**. That's not what you need here. You need a char*. In the function date, the argument char wday[] is the same as char* wday.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

Question code is very close to functional;


Change:

void date(datum *d, char wday[])

To:

void date(datum *d, char **wday)

Change:

char wday[]="";

To:

char *wday="";

After these changes, it compiles without warnings or errors, and executes as expected.

SLES11SP2:~/SO> gcc -Wall -o test *.c
SLES11SP2:~/SO> ./test
It's Tuesday
SLES11SP2:~/SO>
Mahonri Moriancumer
  • 5,993
  • 2
  • 18
  • 28
0

Changing char wday[]=""; ==> char *wday; and void date(datum *d, char wday[]) ==> void date(datum *d, char *wday[]) would do the trick!

In general, You would need to pass a pointer to pointer (or pointer to array) because you are mutating the input in the called function. If you would have just needed a copy of the character array in your called function only then would you pass pointer to char array like this char *array or char array[].

Hope this helps!