SlideShare una empresa de Scribd logo
1 de 16
An Introduction to Pointers
• A pointer(address variable) is a variable whose value is used to point to
another variable.
Ex:
long int x, y;
y = &x;
(assigns address of the long integer variable x to another variable, y.)
Ex:
#include <stdio.h>
main()
{
char c;
int x;
float y;
printf("c: address=0x%p, content=%cn", &c, c);
printf("x: address=0x%p, content=%dn", &x, x);
printf("y: address=0x%p, content=%5.2fn", &y, y);
c = `A';
x = 7;
y = 123.45;
printf("c: address=0x%p, content=%cn", &c, c);
printf("x: address=0x%p, content=%dn", &x, x);
printf("y: address=0x%p, content=%5.2fn", &y, y);
return 0;
}
NOTE: %p, %u, %lu
• Pointer has Left value and Right value.
– Left value: address its self
– Right value: which is the content of the pointer, is the address of another variable.
– data-type *pointer-name;
• Ex:
#include <stdio.h>
main()
{
char c, *ptr_c;
int x, *ptr_x;
float y, *ptr_y;
c = `A';
x = 7;
y = 123.45;
printf("c: address=0x%p, content=%cn", &c, c);
printf("x: address=0x%p, content=%dn", &x, x);
printf("y: address=0x%p, content=%5.2fn", &y, y);
ptr_c = &c;
printf("ptr_c: address=0x%p, content=0x%pn", &ptr_c, ptr_c);
printf("*ptr_c => %cn", *ptr_c);
ptr_x = &x;
printf("ptr_x: address=0x%p, content=0x%pn", &ptr_x, ptr_x);
printf("*ptr_x => %dn", *ptr_x);
ptr_y = &y;
printf("ptr_y: address=0x%p, content=0x%pn", &ptr_y, ptr_y);
printf("*ptr_y => %5.2fn", *ptr_y);
return 0;
}
Storing Similar Data Items
• array is a collection of variables that are of the same data type.
– data-type Array-Name[Array-Size];
EX:
#include <stdio.h>
main()
{
int i;
int list_int[10];
for (i=0; i<10; i++){
list_int[i] = i + 1;
printf( "list_int[%d] is initialized with %d.n", i, list_int[i]);
}
return 0;
}
One dimensionl Array
• One dimensional array
– Int a[10], float x[50], char str[30];
– Int a[10]={12,3,4,65,6,2,3}; int
a[]={1,2,3,4,5,6,7,8};
– Char str[30]=“Hello how well?”;
– ‘0’=null remain
Ex:
#include <stdio.h>
main()
{
char array_ch[7] = {`H', `e', `l', `l', `o', `!’};
int i;
for (i=0; i<7; i++)
printf("array_ch[%d] contains: %cn", i, array_ch[i]);
/*--- method I ---*/
printf( "Put all elements together(Method I):n");
for (i=0; i<7; i++)
printf("%c", array_ch[i]);
/*--- method II ---*/
printf( "nPut all elements together(Method II):n");
printf( "%sn", array_ch);
return 0;
}
Ex:
#include <stdio.h>
main()
{
char array_ch[15] = {`C', ` `,
`i', `s', ` `,
`p', `o', `w', `e', `r',
`f', `u', `l', `!', `0'};
int i;
/* array_ch[i] in logical test */
for (i=0; array_ch[i] != `0'; i++)
printf("%c", array_ch[i]);
return 0;
}
INSERT, SEARCH, SORT, DELETE in one
demensional Array
Multidimensional Arrays
• data-type Array-Name[Array-Size1][Array-
Size2]. . .[Array-SizeN];
– int array_int[2][3];
– int array_int[2][3] = {{1, 2, 3}, {4, 5, 6}};
Unsized Arrays
• int list_int[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90};
• char list_ch[][2] = { `7', `A', `b', `B',`c', `C',`d',
`D',`e', `E', `f', `F',`g', `G'};
Manipulating Strings
• a string is a character array terminated by a
null character (0).
• char array_ch[7] = {`H', `e', `l', `l', `o', `!', `0'};
• char str[7] = "Hello!";
• char str[] = "I like C.";
• char *ptr_str = "I teach myself C.";
• char ch = `x';
• char str[] = "x";
• char *ptr_str;
ptr_str = "A character string.";
#include <stdio.h>
main()
{
char str1[] = {`A', ` `,
`s', `t', `r', `i', `n', `g', ` `,
`c', `o', `n', `s', `t', `a', `n', `t', `0'};
char str2[] = "Another string constant";
char *ptr_str;
int i;
/* print out str2 */
for (i=0; str1[i]; i++)
printf("%c", str1[i]);
printf("n");
/* print out str2 */
for (i=0; str2[i]; i++)
printf("%c", str2[i]);
printf("n");
/* assign a string to a pointer */
ptr_str = "Assign a string to a pointer.";
for (i=0; *ptr_str; i++)
printf("%c", *ptr_str++);
return 0;
}
#include <stdio.h>
main()
{
char str[80];
int i, delt = `a' - `A';
printf("Enter a string less than 80 characters:n");
gets( str );
i = 0;
while (str[i]){
if ((str[i] >= `a') && (str[i] <= `z'))
str[i] -= delt; /* convert to upper case */
++i;
}
printf("The entered string is (in uppercase):n");
puts( str );
return 0;
}
char *str = "Hello World!";
• Alternatively one might think that a pointer to string in C is a pointer to a char array
/ a pointer to a pointer to a char.
• char *str = "Hello World!";
– str is a pointer to a string.
– str is a pointer to a char array.
– But str is a pointer to a char (not a pointer to a pointer to a
char). str is of type char * .(and here str points to H).
– Ended by 0 null
– char *v1=“A”; 2 bytes
– char v1=‘A’; 1 byte
– ‘A’ + 20 + ‘C’ but not “A” + 20 + “C”
H e l l o W o r l ! 0
A 0
A
Arr[6]=“12345”;
• char arr[6]=“gfhgf”;
• char arr[6]={‘1’,’2’,’3’,’4’,’5’};
• char arr[]=“3212432432”;
1 2 3 4 5 0

Más contenido relacionado

La actualidad más candente

C Prog. - Strings (Updated)
C Prog. - Strings (Updated)C Prog. - Strings (Updated)
C Prog. - Strings (Updated)
vinay arora
 
C Prog. - Structures
C Prog. - StructuresC Prog. - Structures
C Prog. - Structures
vinay arora
 
C Prog - Strings
C Prog - StringsC Prog - Strings
C Prog - Strings
vinay arora
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
Azhar Javed
 

La actualidad más candente (20)

C Prog - Array
C Prog - ArrayC Prog - Array
C Prog - Array
 
C Prog. - Strings (Updated)
C Prog. - Strings (Updated)C Prog. - Strings (Updated)
C Prog. - Strings (Updated)
 
C Prog. - Structures
C Prog. - StructuresC Prog. - Structures
C Prog. - Structures
 
Implementing string
Implementing stringImplementing string
Implementing string
 
C PROGRAMS
C PROGRAMSC PROGRAMS
C PROGRAMS
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
 
Double linked list
Double linked listDouble linked list
Double linked list
 
C questions
C questionsC questions
C questions
 
C Prog - Strings
C Prog - StringsC Prog - Strings
C Prog - Strings
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
 
C programms
C programmsC programms
C programms
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
 
ADA FILE
ADA FILEADA FILE
ADA FILE
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
 
BCSL 058 solved assignment
BCSL 058 solved assignmentBCSL 058 solved assignment
BCSL 058 solved assignment
 
Array
ArrayArray
Array
 
C programming
C programmingC programming
C programming
 
Stl algorithm-Basic types
Stl algorithm-Basic typesStl algorithm-Basic types
Stl algorithm-Basic types
 
Double linked list
Double linked listDouble linked list
Double linked list
 
C Programming Language Step by Step Part 5
C Programming Language Step by Step Part 5C Programming Language Step by Step Part 5
C Programming Language Step by Step Part 5
 

Similar a 4. chapter iii

C basics
C basicsC basics
C basics
MSc CST
 
C Prog - Pointers
C Prog - PointersC Prog - Pointers
C Prog - Pointers
vinay arora
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
Er Ritu Aggarwal
 
(Meta 5) ejemplo vectores dev c++
(Meta 5) ejemplo vectores dev c++ (Meta 5) ejemplo vectores dev c++
(Meta 5) ejemplo vectores dev c++
Eli Diaz
 
(Meta 5) ejemplo vectores 2 dev c++
(Meta 5) ejemplo vectores 2 dev c++ (Meta 5) ejemplo vectores 2 dev c++
(Meta 5) ejemplo vectores 2 dev c++
Eli Diaz
 

Similar a 4. chapter iii (20)

C basics
C basicsC basics
C basics
 
12 1 문자열
12 1 문자열12 1 문자열
12 1 문자열
 
Cpd lecture im 207
Cpd lecture im 207Cpd lecture im 207
Cpd lecture im 207
 
C Prog - Pointers
C Prog - PointersC Prog - Pointers
C Prog - Pointers
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
Array Programs.pdf
Array Programs.pdfArray Programs.pdf
Array Programs.pdf
 
Data Structure in C Programming Language
Data Structure in C Programming LanguageData Structure in C Programming Language
Data Structure in C Programming Language
 
C Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossainC Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossain
 
Unit 3 Input Output.pptx
Unit 3 Input Output.pptxUnit 3 Input Output.pptx
Unit 3 Input Output.pptx
 
LAB PROGRAMS SARASWATHI RAMALINGAM
LAB PROGRAMS SARASWATHI RAMALINGAMLAB PROGRAMS SARASWATHI RAMALINGAM
LAB PROGRAMS SARASWATHI RAMALINGAM
 
(Meta 5) ejemplo vectores dev c++
(Meta 5) ejemplo vectores dev c++ (Meta 5) ejemplo vectores dev c++
(Meta 5) ejemplo vectores dev c++
 
(Meta 5) ejemplo vectores 2 dev c++
(Meta 5) ejemplo vectores 2 dev c++ (Meta 5) ejemplo vectores 2 dev c++
(Meta 5) ejemplo vectores 2 dev c++
 
Ds
DsDs
Ds
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
Strings in C
Strings in CStrings in C
Strings in C
 
String
StringString
String
 
Examples sandhiya class'
Examples sandhiya class'Examples sandhiya class'
Examples sandhiya class'
 
DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
 

Más de Chhom Karath (20)

set1.pdf
set1.pdfset1.pdf
set1.pdf
 
Set1.pptx
Set1.pptxSet1.pptx
Set1.pptx
 
orthodontic patient education.pdf
orthodontic patient education.pdforthodontic patient education.pdf
orthodontic patient education.pdf
 
New ton 3.pdf
New ton 3.pdfNew ton 3.pdf
New ton 3.pdf
 
ច្បាប់ញូតុនទី៣.pptx
ច្បាប់ញូតុនទី៣.pptxច្បាប់ញូតុនទី៣.pptx
ច្បាប់ញូតុនទី៣.pptx
 
Control tipping.pptx
Control tipping.pptxControl tipping.pptx
Control tipping.pptx
 
Bulbous loop.pptx
Bulbous loop.pptxBulbous loop.pptx
Bulbous loop.pptx
 
brush teeth.pptx
brush teeth.pptxbrush teeth.pptx
brush teeth.pptx
 
bracket size.pptx
bracket size.pptxbracket size.pptx
bracket size.pptx
 
arch form KORI copy.pptx
arch form KORI copy.pptxarch form KORI copy.pptx
arch form KORI copy.pptx
 
Bracket size
Bracket sizeBracket size
Bracket size
 
Couple
CoupleCouple
Couple
 
ច្បាប់ញូតុនទី៣
ច្បាប់ញូតុនទី៣ច្បាប់ញូតុនទី៣
ច្បាប់ញូតុនទី៣
 
Game1
Game1Game1
Game1
 
Shoe horn loop
Shoe horn loopShoe horn loop
Shoe horn loop
 
Opus loop
Opus loopOpus loop
Opus loop
 
V bend
V bendV bend
V bend
 
Closing loop
Closing loopClosing loop
Closing loop
 
Maxillary arch form
Maxillary arch formMaxillary arch form
Maxillary arch form
 
Front face analysis
Front face analysisFront face analysis
Front face analysis
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 

4. chapter iii

  • 2.
  • 3. • A pointer(address variable) is a variable whose value is used to point to another variable. Ex: long int x, y; y = &x; (assigns address of the long integer variable x to another variable, y.) Ex: #include <stdio.h> main() { char c; int x; float y; printf("c: address=0x%p, content=%cn", &c, c); printf("x: address=0x%p, content=%dn", &x, x); printf("y: address=0x%p, content=%5.2fn", &y, y); c = `A'; x = 7; y = 123.45; printf("c: address=0x%p, content=%cn", &c, c); printf("x: address=0x%p, content=%dn", &x, x); printf("y: address=0x%p, content=%5.2fn", &y, y); return 0; } NOTE: %p, %u, %lu
  • 4. • Pointer has Left value and Right value. – Left value: address its self – Right value: which is the content of the pointer, is the address of another variable. – data-type *pointer-name; • Ex: #include <stdio.h> main() { char c, *ptr_c; int x, *ptr_x; float y, *ptr_y; c = `A'; x = 7; y = 123.45; printf("c: address=0x%p, content=%cn", &c, c); printf("x: address=0x%p, content=%dn", &x, x); printf("y: address=0x%p, content=%5.2fn", &y, y); ptr_c = &c; printf("ptr_c: address=0x%p, content=0x%pn", &ptr_c, ptr_c); printf("*ptr_c => %cn", *ptr_c); ptr_x = &x; printf("ptr_x: address=0x%p, content=0x%pn", &ptr_x, ptr_x); printf("*ptr_x => %dn", *ptr_x); ptr_y = &y; printf("ptr_y: address=0x%p, content=0x%pn", &ptr_y, ptr_y); printf("*ptr_y => %5.2fn", *ptr_y); return 0; }
  • 5. Storing Similar Data Items • array is a collection of variables that are of the same data type. – data-type Array-Name[Array-Size]; EX: #include <stdio.h> main() { int i; int list_int[10]; for (i=0; i<10; i++){ list_int[i] = i + 1; printf( "list_int[%d] is initialized with %d.n", i, list_int[i]); } return 0; }
  • 6. One dimensionl Array • One dimensional array – Int a[10], float x[50], char str[30]; – Int a[10]={12,3,4,65,6,2,3}; int a[]={1,2,3,4,5,6,7,8}; – Char str[30]=“Hello how well?”; – ‘0’=null remain
  • 7. Ex: #include <stdio.h> main() { char array_ch[7] = {`H', `e', `l', `l', `o', `!’}; int i; for (i=0; i<7; i++) printf("array_ch[%d] contains: %cn", i, array_ch[i]); /*--- method I ---*/ printf( "Put all elements together(Method I):n"); for (i=0; i<7; i++) printf("%c", array_ch[i]); /*--- method II ---*/ printf( "nPut all elements together(Method II):n"); printf( "%sn", array_ch); return 0; } Ex: #include <stdio.h> main() { char array_ch[15] = {`C', ` `, `i', `s', ` `, `p', `o', `w', `e', `r', `f', `u', `l', `!', `0'}; int i; /* array_ch[i] in logical test */ for (i=0; array_ch[i] != `0'; i++) printf("%c", array_ch[i]); return 0; }
  • 8. INSERT, SEARCH, SORT, DELETE in one demensional Array
  • 9. Multidimensional Arrays • data-type Array-Name[Array-Size1][Array- Size2]. . .[Array-SizeN]; – int array_int[2][3]; – int array_int[2][3] = {{1, 2, 3}, {4, 5, 6}};
  • 10. Unsized Arrays • int list_int[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90}; • char list_ch[][2] = { `7', `A', `b', `B',`c', `C',`d', `D',`e', `E', `f', `F',`g', `G'};
  • 11. Manipulating Strings • a string is a character array terminated by a null character (0). • char array_ch[7] = {`H', `e', `l', `l', `o', `!', `0'}; • char str[7] = "Hello!"; • char str[] = "I like C."; • char *ptr_str = "I teach myself C.";
  • 12. • char ch = `x'; • char str[] = "x"; • char *ptr_str; ptr_str = "A character string.";
  • 13. #include <stdio.h> main() { char str1[] = {`A', ` `, `s', `t', `r', `i', `n', `g', ` `, `c', `o', `n', `s', `t', `a', `n', `t', `0'}; char str2[] = "Another string constant"; char *ptr_str; int i; /* print out str2 */ for (i=0; str1[i]; i++) printf("%c", str1[i]); printf("n"); /* print out str2 */ for (i=0; str2[i]; i++) printf("%c", str2[i]); printf("n"); /* assign a string to a pointer */ ptr_str = "Assign a string to a pointer."; for (i=0; *ptr_str; i++) printf("%c", *ptr_str++); return 0; }
  • 14. #include <stdio.h> main() { char str[80]; int i, delt = `a' - `A'; printf("Enter a string less than 80 characters:n"); gets( str ); i = 0; while (str[i]){ if ((str[i] >= `a') && (str[i] <= `z')) str[i] -= delt; /* convert to upper case */ ++i; } printf("The entered string is (in uppercase):n"); puts( str ); return 0; }
  • 15. char *str = "Hello World!"; • Alternatively one might think that a pointer to string in C is a pointer to a char array / a pointer to a pointer to a char. • char *str = "Hello World!"; – str is a pointer to a string. – str is a pointer to a char array. – But str is a pointer to a char (not a pointer to a pointer to a char). str is of type char * .(and here str points to H). – Ended by 0 null – char *v1=“A”; 2 bytes – char v1=‘A’; 1 byte – ‘A’ + 20 + ‘C’ but not “A” + 20 + “C” H e l l o W o r l ! 0 A 0 A
  • 16. Arr[6]=“12345”; • char arr[6]=“gfhgf”; • char arr[6]={‘1’,’2’,’3’,’4’,’5’}; • char arr[]=“3212432432”; 1 2 3 4 5 0

Notas del editor

  1. #include<stdio.h> #include<conio.h> void main() { clrscr(); int k,n; int d[60]; printf("Enter n:"); scanf("%d",&n); //insert for(int i=0;i<n;i++) { printf("Enter d[%d]=",i); scanf("%d",&d[i]); } //duplicate for( i=0;i<n-1;i++) for(int j=i+1;j<n;j++) { if(d[i]==d[j]) { n=n-1; for(k=j;k<n;k++) { d[k]=d[k+1]; } j--; } } //sort DESC int t; for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) if(d[i]<d[j]) { t=d[i]; d[i]=d[j]; d[j]=t; } //Delete int dn; printf("Enter number to delete:");scanf("%d",&dn); for(i=0;i<n;i++) { if(dn==d[i]) { n=n-1; for(j=i;j<n;j++) d[j]=d[j+1]; i--; } } //search int b=1; int sn; printf("Enter number to search:");scanf("%d",&sn); for(i=0;i<n;i++) { if(sn==d[i]) b=0; } if(b==1) { printf("Not Found"); } else { printf("I Found"); } //output for(i=0;i<n;i++) { printf("d[%d]=%d\n",i,d[i]); } getch(); }