Using pointers in C language to simulate passing by reference (case-insensitive conversion of string letters)

Source: Internet
Author: User

For more information, visit: http://www.xcoder.cn

The function of ansi c only supports passing by value, but does not support transferring by reference. However, the pointer operation can simulate passing by reference. In fact, it is the value of the passed pointer, the function uses a pointer to modify the value of the pointer to the address:

 

# Include <stdio. h> <br/> # include <ctype. h> <br/> void converttouppercase (char *); <br/> void main () {<br/> char string [] = "characters and & 32.98 "; <br/> printf ("the string before conversion is: % s", string); <br/> converttouppercase (string ); <br/> printf ("/nthe string after conversion is: % s/n", string); <br/>}< br/> void converttouppercase (char * sptr) {<br/> while (* sptr! = '/0') {<br/> If (islower (* sptr) <br/> * sptr = toupper (* sptr ); </P> <p> sptr ++; <br/>}< br/>}

 

The running result is as follows:

 

the string before conversion is: characters and $32.98 <br/> the string after conversion is: characters and $32.98

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.