1 / 9

Pointers and Strings in Windows Programming

Learn about pointers and strings in Windows programming using C language. Understand the basics of pointers, declaration of pointers, pointer operators, pointers and arrays, dynamic allocation, characters and strings, and AnsiString class.

dwaynes
Télécharger la présentation

Pointers and Strings in Windows Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 10 Pointers and Strings Windows Programming, C.-S. Shieh, KUAS EC, 2005

  2. 10.1 What Is a Pointer? • A pointer is a variable containing the address of another variable. Windows Programming, C.-S. Shieh, KUAS EC, 2005

  3. 10.2 Declaration of Pointers int i; float x; int* ip; float* xp; Windows Programming, C.-S. Shieh, KUAS EC, 2005

  4. 10.3 Pointer Operators int* ip; Int i,j; i=3; ip=i; // Illegal ip=&i; // &: 取址運算子 (*ip)=5; // *: 間接運算子 ip=&j; printf(”%d”,i); Windows Programming, C.-S. Shieh, KUAS EC, 2005

  5. 10.4 Pointers and Arrays • Static Allocation • float x[10]; • x is actually a pointer pointing to the first element. • *(x+i) is equivalent to x[i] • Array as parameter in function call is in effect call-by-address. Windows Programming, C.-S. Shieh, KUAS EC, 2005

  6. 10.4 Pointers and Arrays (cont) • Dynamic Allocation #include <stdio.h> #include <stdlib.h> void main(void) { int i,j; float* xp; scanf("%d",&i); xp=(float*)malloc(i*sizeof(float)); for(j=0;j<i;j++) *(xp+j)=j; for(j=0;j<i;j++) printf("%f ",*(xp+j)); } Windows Programming, C.-S. Shieh, KUAS EC, 2005

  7. 10.5 Characters and Strings • In BASIC DIM str1 AS STRING DIM str2 AS STRING DIM str3 AS STRING str1 = "Hello" str2 = "world" str3 = str1 + ", " + str2 + "!" PRINT str3 Windows Programming, C.-S. Shieh, KUAS EC, 2005

  8. 10.5 Characters and Strings (cont) • There is no “string” type in C, but array of characters. char str1[16]= ”love”; // Only valid in declaration char str2[16]; str2= ” hello”; // Illegal strcpy(str2,”hello”); // Correct str1[0]=‘a’;str1[1]=‘b’;str1[2]=‘c’;str1[3]=‘\0’; printf(“%s”,str1); • String function • strlen(), strcpy(), strcat(), strcmp() Windows Programming, C.-S. Shieh, KUAS EC, 2005

  9. 10.6 AnsiString Class • AnsiString class facilitate string processing. AnsiString s1,s2,s3; s1= ”Hello”; s2= ”world”; s3=s1+”, ”+s2+”!”; • Member Functions • c_str(), Length() FILE* fileptr; OpenDialog1->Execute(); fileptr=fopen((OpenDialog1->FileName).c_str(),"w"); StrPas() Windows Programming, C.-S. Shieh, KUAS EC, 2005

More Related