Task: Write a program that uses a pointer to a character string in a function to determine the number of characters in a string using structures.
The compiler throws an error:
error: invalid conversion from 'char' to 'const char*' [-fpermissive]
8 | cout << strlen(a);
| ^
| |
| char
This is my code:
#include <iostream>
#include <cstring>
using namespace std;
void func(char a)
{
cout << strlen(a);
}
int main()
{
struct student
{
char name[64];
};
student student1;
cin >> student1.name;
char* ptr = &student1.name[64];
func(*ptr);
return 0;
}
There are several invalid constructions in the program.
You have at least to write instead
void func( const char *a )
^^^^^^^^^^^^^^
and
char* ptr = student1.name;
func(ptr);
And instead of
cin >> student1.name;
it is better to use
cin.get( student1.name, sizeof( student1.name ) );
Also it seems you are not allowed to use the standard C function strlen
and must write an equivalent function yourself.