C++ strings, character arrays and character pointers (*, **)

C++ strings, character arrays and character pointers (*, **)

  • Strings + pointers have been used frequently recently. Sometimes I want to get values ​​or copy, and often encounter problems. Let’s summarize them here.
  • String processing and pointer use
  • Long term updated version~

1. char usage related

1.1 Memory usage

  • First, let’s introduce the data types in C language:
    Insert image description here
  • The figure below shows the number of bytes occupied by different data types. The storage size of various types is related to the number of system bits, but currently the most common ones are 64-bit systems. The following lists the differences in storage size between 32-bit systems and 64-bit systems (same for Windows):Insert image description here
#include <stdio.h>
#include <float.h>
 
int main()
{
    
    
   printf("float 存储最大字节数 : %lu \n", sizeof(float));
   printf("float 最小值: %E\n", FLT_MIN );
   printf("float 最大值: %E\n", FLT_MAX );
   printf("精度值: %d\n", FLT_DIG );
   
   return 0;
}


**********output***********
float 存储最大字节数 : 4 
float 最小值: 1.175494E-38
float 最大值: 3.402823E+38
精度值: 6

1.2 Pointers

  • Pointers are memory addresses, and pointer variables are variables used to store memory addresses. Just like any other variable or constant, you must declare a pointer before using it to store the address of another variable. The general form of pointer variable declaration is:
//type *var_name;
char* data;  
char** data_locatioin
int* abc;
  • for example:
#include <stdio.h>
 
int main ()
{
    
    
    int var_runoob = 10;
    int *p;              // 定义指针变量
    p = &var_runoob;
 
   printf("var_runoob 变量的地址: %p\n", p);
   return 0;
}

*********output***********
var_runoob 变量的地址: 0x7ffeeaae08d8

Insert image description here

1.3 String

  • A string is actually a one-dimensional character array terminated with the null character \0. Therefore, \0 is used to mark the end of a string.
  • The declaration/definition is as follows:
char site[7] = {
    
    'R', 'U', 'N', 'O', 'O', 'B', '\0'};
char site[] = "RUNOOB";

Insert image description here

#include <stdio.h>
 
int main ()
{
    
    
   char site[7] = {
    
    'R', 'U', 'N', 'O', 'O', 'B', '\0'};
 
   printf("菜鸟教程: %s\n", site );
 
   char str1[14] = "runoob";
   char str2[14] = "google";
   char str3[14];
   int  len ;
   /* 复制 str1 到 str3 */
   strcpy(str3, str1);
   printf("strcpy( str3, str1) :  %s\n", str3 );
 
   /* 连接 str1 和 str2 */
   strcat( str1, str2);
   printf("strcat( str1, str2):   %s\n", str1 );
 
   /* 连接后,str1 的总长度 */
   len = strlen(str1);
   printf("strlen(str1) :  %d\n", len );
   return 0;
}

1.4 Character pointer

  • A pointer is a variable used to store an address. The address uniquely identifies a memory space; the size of the pointer is a fixed 4/8 bytes (32 platform/64 platform).
char ch = 'w';	//字符变量ch
char* pc=&ch;	//将字符变量ch的地址取出来,存在pc中。pc就被称为字符指针,类型就是char

--->
*pc = 'w';
  • assigns the string to a character pointer variable p. Instead of assigning the content of the string to pstr, it assigns the address of the first character of the string to pstr.
int main(){
    
    
    char* pstr="hello";
    printf("%s\n",pstr);
    return 0;
}
  • Print:
  • To print a character, use %c. The address of a is stored in p. *p is a.
  • Print the entire character and stop when **"\0"** is encountered. Use %s. The address of a is stored in p. Simply put p at the end and print a string starting from the address where p is stored, and "abcdef" will be printed.
int main() {
    
    
	 char arr[] = "abcdef";	//字符串存入arr数组里面
 	 char* pc=arr;//pc字符指针存放数组名,即首元素地址。
	 printf("%s\n", arr);   //abcdef
	 printf("%s\n", pc);    //abcdef
	 printf("%c\n",*pc);    //a 
     printf("%s\n", pc+2);  //cdef
     
     for(int i=0;i<7;i++)
     {
    
    
         printf("%c",*(pc+i));
     }
	 return 0;
}

***********output**********
abcdef
abcdef

2. Character pointer parameter passing

  • Usually image data will be defined as pointer storage of type uchar/unsigned char. The calling method is given below:
#include<opencv/opencv2.hpp>

int add(uchar*srcdata1,uchar*srcdata2,uchar*dstdata,int h,int w,int c)
{
    
    
    if(c==3)
    {
    
    
        cv::Mat src1=cv::Mat(h,w,CV_8UC3,srcdata1);
    }
    .....
}
  • But today I encountered a problem with pointer parameter passing. The interface in the C++ encapsulated dll is called through the C++ exe to update the pointer data/image data.
//DLL中函数定义:
int DLL WINAPI imgGrab(unsigned char* imgdata)
{
    
    
    imgdata=getNextFrame();
}

//exe中:
main()
{
    
    
    uchar* data=new uchar[h*w*c];
    imgGrab(data);
}
------如上方法调用时,在dll中图像数据正常,但是传递出来的图像数据全为0,异常---
---->然后做了如下修改:
//DLL中函数定义:
int DLL WINAPI imgGrab(unsigned char** imgdata)
{
    
    
    *imgdata=getNextFrame();
}

//exe中:
main()
{
    
    
    uchar* data=new uchar[h*w*c];
    imgGrab(&data);
}
------->解决!

Guess you like

Origin blog.csdn.net/yohnyang/article/details/134450614