如何打印输入数组中每个字符的地址和内容

时间:2014-11-18 15:08:17

标签: c arrays function pointers

我需要做的是遍历数组' location'打印每个元素的地址和内容。该函数在main' charAddresses'。

之外调用

我问我的代码是否正确尚未实现?

#include "stdafx.h"
#include <stdio.h>
#include <string.h>

void charAddresses(char *string); // 1st function to do


int main()
{
    int i = 0;
    char location[200] = "7825,CREEK VALLEY,SACRAMENTO,95828,CA";
    //char *ptr;

    // 1. call function to print out the address and content of each character in the input array
    charAddresses(location);

}
void charAddresses(char *string)
{
    int i;

    for (i = 0; *(string + i) != '\0'; i++)
        //for (i = 0; i <strlen(string); i++)
    {
        //printf("Address is:%p\t Char is:%c", *ptr, location[i])
        printf("Address is %p\t, Char is: %c\n", string + i, string[i]);
    }
}

1 个答案:

答案 0 :(得分:0)

感谢@chux的线索,完成的代码;

#include "stdafx.h"
#include <stdio.h>
#include <string.h>

void charAddresses(char *string); // 1st function to do


int main()
{
    int i = 0;
    char location[200] = "7825,CREEK VALLEY,SACRAMENTO,95828,CA";
    //char *ptr;

    // 1. call function to print out the address and content of each character in the input array
    charAddresses(location);

}
void charAddresses(char *string)
{
    int i;

    for (i = 0; *(string + i) != '\0'; i++)
        //for (i = 0; i <strlen(string); i++)
    {
        //printf("Address is:%p\t Char is:%c", *ptr, location[i])
        printf("Address is %p\t, Char is: %c\n", string + i, string[i]);
    }
}