LearnFrenzy
Sign in Take a free test

Typedef · Find the Error · Question 3

Error: #define used as a pointer type for two variables Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 3 Back to Exercise

The program below has a bug. Which statement correctly describes it?

main.c
#include <stdio.h>

#define CHARPTR char *

int main(void) {
    CHARPTR first, second;
    first = "alpha";
    second = "beta";
    printf("%s %s\n", first, second);
    return 0;
}
  • A String literals cannot be assigned to pointers
  • B The macro makes only first a pointer; second is a char, so second = "beta" is invalid – use typedef char *CharPtr; Correct
  • C printf needs %c
  • D There is no bug
Correct Answer: Option B

After expansion the line is char * first, second;. Assigning a pointer to the char second is rejected. A typedef applies the pointer to every declarator.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links