It is clogged with C-language pointers. [Closed]

Asked 2 years ago, Updated 2 years ago, 31 views

Do you want to improve this question?Edit your post to clarify the issue you are trying to resolve by adding details.

Closed 5 years ago.

5 years ago

Compilation error occurs below.

#include<stdio.h>
# include <stdlib.h>
# include <string.h>

int main() {
    char*buf="<title>sample</title>";
    char work [256];
    char*p;

    char*out=work;
    char*in=buf;

    in = strstr(in, "<");
    while(strcmp((*p=(*out++=*in++))), "sample")!=0);
    printf("%c",p);

    // while(strcmp((*out++=*in++), ">")!=0);
    *out='\0';
    printf("%s\n", out);
    return 0;
}

The following error appears:

01.c:In function 'main':
01.c:14:2:warning:passing argument 1 of 'strcmp' makes pointer from integer without a cast [enabled by default]
  while(strcmp((*p=(*out++=*in++))), "sample")!=0);
  ^
Infile included from 01.c:3:0:
c:\mingw\include\string.h:43:37:note:expected 'const char*' but argument is of type 'char'
 _CRTIMP int__cdecl_MINGW_NOTHROW strcmp(const char*, const char*)_MINGW_ATTRIB_PURE;
                                     ^

I'm sorry.Please point out the mistake.
If possible, please reply with the code.
That's faster to understand.Thank you for your cooperation.

c

2022-09-29 21:41

1 Answers

Calm down and read the warnings and errors.
The strcmp function takes the char pointer type as the first argument, while *p is the char variable.

Also, the next line is

printf("%c",p);

%c is used to display char variables, but p is not correct because it is a char pointer type. Use %p if you want to display pointer values and %s if you want to display NULL termination strings.

char type and char pointer type seem to be confused. Check the difference between the two.


2022-09-29 21:41

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.