0

Hello here is my code:

string num_s[slash_pos];
strncpy(num_s, fraction + 0, slash_pos - 0);

where slash_pos = 2, and string fraction = "11/89". I want to get that numerator string "11". I want to make it dynamic, and thus instead of assigning things as such:

num_s[0] = fraction[0] num_s[1] = fraction[1]

However I'm getting this error: incompatible pointer types passing 'string [slash_pos ]' to parameter of type 'char *

What is wrong?

1 Answer 1

0

You've declared an array of strings. I think you meant to declare an array of chars (one string)?

Also, to be clear, for this part of the problem, you are told that you can assume that the string given to you will be in the format X/Y where both X and Y are single digits and X/Y works out to a whole number of eighths.

There's no need to create any new strings, as you only need to deal with the numerator (as a single digit) and the denominator (as a single digit).
Recall that you can access a single char like fraction[0] or fraction[2] and get the integer value using the same technique you might have used in caesar/vigenere.

2
  • yes, I know that X and Y are single digits; i had that in mind when coding this. But, I wanted to try it this way because it makes the program more dynamic (because some time durations can be MORE than one digit for example: (1/32)). Thus, how do I fix this such that I make an array of chars (one string) of length slash_pos where slash_pos is an integer? Jun 25, 2018 at 16:59
  • If you want an array of chars, then you need to declare it as such: char num_s[slash_pos]; Keep in mind that the duration function must return an int representing the number of eighths, so a duration of "1/32" cannot be handled by the program you are given.
    – curiouskiwi
    Jun 25, 2018 at 21:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .