gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:30:16 25 4
gpt4 key购买 nike

我需要做的是遍历数组“位置”,打印每个元素的地址和内容。该函数在主“ 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]);
}
}

最佳答案

感谢@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]);
}
}

关于c - 如何在输入数组中打印出每个字符的地址和内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26997510/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com