[C language] Summary of problem points

1. When watching the video, there is such a point of view, the video connection: The video connection of station B is at 35 minutes . That is, when defining the array, you cannot use variables. The code is as follows. The video says that VS2012 compilation will report an error, but I use Dev-c++ There was no error when compiling, and the compiling and running passed. For this problem, I haven't figured it out yet?

#include <stdio.h>
int main() 
{
    
    
	int num=4;
	int a[num]={
    
    1,2,3,4};
	return 0;
}

2. It strlenis used to calculate the length of the string,
but sometimes the compiler will report an error 'strlen' was not declared in this scope, as shown below:

At this time, you only need to add the header file #include <string.h>to solve the problem.

#include <stdio.h>
#include <string.h>
int main() 
{
    
    
	char a1[]={
    
    "abc"};//数组 
	//"abc"---'a''b''c''\0'--'\0'字符串的结束标志 
	char a2[]={
    
    'a','b','c','\0'};
	char a3[]={
    
    'a','b','c'};
	printf("%d\n",strlen(a1));//strlen--计算字符串长度的 
	printf("%d\n",strlen(a2));
	printf("%d\n",strlen(a3));
	return 0;
}

3.报错C [Warning] deprecated conversion from string constant to ‘char*’

#include "stdio.h"
char *str1 = "Hello";
char  str2[] = "Hello";
int main()
{
    
    
	printf("%d %d", sizeof(str1), sizeof(str2));
}

The solution is as follows: add a const

#include "stdio.h"
const char *str1 = "Hello";
char  str2[] = "Hello";
int main()
{
    
    
	printf("%d %d", sizeof(str1), sizeof(str2));
}

Guess you like

Origin blog.csdn.net/wsq_666/article/details/114596207