Home Forums C Programming How do I use char* variable type in C?

Viewing 4 reply threads
  • Author
    Posts
    • #2026
      divyak kumar
      Participant

      I am a beginner.The following code in Microsoft Visual C++ 6.0 IDE gives out an error.
      It compiles with no errors and no warnings.
      Then makes with no errors and no warnings.
      But displays a dialog box showing that there is an error and asking to report it to Microsoft.

      What is the correct usage of char*?
      Can anybody please help me?

      • This topic was modified 9 months, 4 weeks ago by M. Saqib.
      • This topic was modified 9 months, 4 weeks ago by M. Saqib.

    • #3266
      niklaesh
      Participant

      Hello
      try this code.

      As you have declared a pointer which points a character.
      char* yourName;
      Now you are getting a value from the user and trying to store it in pointer type variable. scanf() function takes the address of the variable i.e. pointer to that variable and store the input. As you have already declared the pointer and you code scanf(“%s”, &yourName); is trying to save that input value in the address of that pointer. Which is logically incorrect. Your code looks fine and no error but it has a logical mistake that is why it compiles fine but gives error at runtime. You code should look like scanf(“%s”, yourName);
       

      • This reply was modified 9 years, 6 months ago by M. Saqib.
      • This reply was modified 9 months, 4 weeks ago by M. Saqib.
    • #3267
      divyak kumar
      Participant

      Thanks!
      That works well!!

      Thank you for taking your time, and nice brief explanation.

    • #3268
      liban
      Participant

      hi,sakuntala how are you ?what are you doing?

    • #3269
      divyak kumar
      Participant

      I’m still learning programming.
      Somewhat familiar with Visual Basic 6.0 and trying to learn Visual C++, gcc, and Visual C#.

      I am a first year undergraduate student at University of Colombo School of Computing.

      Cheers!

Viewing 4 reply threads
  • The forum ‘C Programming’ is closed to new topics and replies.