Skip to main content
Engineering LibreTexts

10.5: Processing a (String) Character Array using Pointers

  • Page ID
    76146
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    The following example is just like the previous example, but now the array of characters that make up the string are allocated on the heap. Because heap allocation can be set at runtime, the size of the memory that can be used can be changed at runtime.

    In the following example, a toUpper function is written that converts a string of characters to all uppercase. Note that to keep the function simple, only character data should be passed to this function, and there is no check for valid input. As a result of running this function, all lowercase characters are converted to uppercase, and uppercase characters are not affected. The string as all uppercase is returned.

    .global toUpper 
    .global main 
    .text 
    
    toUpper: 
        #push stack 
        SUB sp, sp, #12 
        STR lr, [sp, #0] 
        STR r4, [sp, #4] 
        STR r5, [sp, #8] 
        
        # Save Base Array and Size to preserved registers 
        MOV r4, r0 
        
        # initialize loop for entering data 
        # r4 - element address 
        # r5 - constant null 
        MOV r5, #0 
        
        startLoop: 
            LDRB r1, [r4, #0] 
            CMP r1, r5 
            BEQ endLoop
            AND r1, r1, #0xdf 
            STRB r1, [r4], #1 
            B startLoop 
        endLoop:
        
        #pop stack 
        LDR lr, [sp, #0] 
        LDR r4, [sp, #4] 
        LDR r5, [sp, #8] 
        ADD sp, sp, #12 
        MOV pc, lr
        
    .data
    
    #end printArrayByIndex
    
    # Main procedure to test printArrayByIndex 
    .text 
    main:
        #push stack 
        SUB sp, sp, #4 
        STR lr, [sp, #0]
        
        # load string 
        LDR r0, =prompt 
        BL printf 
        MOV r0, #40 
        BL malloc 
        MOV r5, r0 
        MOV r1, r0 
        LDR r0, =format 
        BL scanf
        
        MOV r0, r5 
        BL toUpper
        
        # reload base and call function 
        LDR r0, =output 
        MOV r1, r5 
        BL printf
        
        #pop stack 
        LDR lr, [sp, #0] 
        ADD sp, sp, #4 
        MOV pc, lr
    
    .data
        prompt: .asciz "Enter input string: " 
        output: .asciz "\nYour string is %s\n" 
        format: .asciz "%s"
    

    10.5.1 Comments on Program

    This program illustrates two new concepts. The first is the use of malloc to allocate the memory for the string. The malloc function allocates memory on the heap. Heap memory is managed at runtime, so the amount of memory that is allocated can be specified at runtime. This allows variable size data structures, such as arrays and strings, to be created in programs, and thus most arrays and strings are allocated in heap memory. The malloc function takes in a parameter that is the size in r0, and returns a pointer to memory of that size in r0.

    The second new concept is the use of a pointer, and not an index, to walk through the array. This is shown in the following code fragment:

    MOV r5, #0 
    
    startLoop: 
        LDRB r1, [r4, #0] 
        CMP r1, r5 
        BEQ endLoop 
        AND r1, r1, #0xdf 
        STRB r1, [r4], #1 
        B startLoop 
    endLoop:
    

    In this code fragment, r4 starts by pointing to the beginning of the string. Each time through the loop the value in r4 is updated to point to the next character in the string using the STRB with post incrementing, e.g. STRB r1, [r4], #1. When the character at the pointer address in r4 is the character 0, the end of the string is reached, and the loop halts.


    This page titled 10.5: Processing a (String) Character Array using Pointers is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by Charles W. Kann III via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.