[英]passing a pointer to a char array in c
I wrote a small test program to see how I can pass a pointer to a char array to a function. 我写了一个小测试程序,看看如何将指向char数组的指针传递给函数。 This is my code: 这是我的代码:
#include <stdio.h>
#include <string.h>
int scopy(char *org ){
printf("the value of org is %s" , org);
printf("\n");
}
int main(void)
{
char original[11];
strcpy(original , "helloworld");
scopy(original);
printf("the value of original now is %s" , original);
return 0;
}
My knowledge of pointers tells me that org is a pointer that is passed the memory address of original[0] , when I execute this program I observe a few things that has confused me and has caused me to doubt the facts I have learnt about pointers. 我对指针的了解告诉我,org是一个指针,它传递了原始[0]的内存地址,当我执行这个程序时,我发现了一些困扰我的事情,并让我怀疑我对指针学到的事实。 。
when I print org I get the complete word back . 当我打印org时,我得到了完整的单词。 but according to the logic if pointers isnt this supposed to print out the memory address; 但根据逻辑,如果指针不是这应该打印出内存地址;
when I try printing out out[1] , out[2] , codepad tells me that the process has ended abnormally 当我尝试打印出[1],out [2]时,键盘告诉我该过程异常结束
My basic doubt in this question is what exactly is org doing , I understand it is a char pointer pointing to a memory address (which??) . 我在这个问题中的基本疑问是org正在做什么,我理解它是一个指向内存地址的char指针(哪个??)。 Any help would be appreciated . 任何帮助,将不胜感激 。 I am very new to c programming . 我是c编程的新手。
Also when I try a while loop like 当我尝试一下while循环时
while(out[i] !='\0')
printf("%s",out[i]);
it does not terminate at the '\\0' . 它不会在'\\ 0'处终止。 the strcpy documentation states that the '\\0' is copied to the char array. strcpy文档声明'\\ 0'被复制到char数组。
org is a pointer to a char, which is what you're passing in scopy (a pointer to the first element of an array). org是一个指向char的指针,它是你在scopy中传递的(指向数组第一个元素的指针)。 When you use the specifier %s
in printf you are treating a pointer to a char as a string; 当你在printf中使用说明符%s
,你将一个指向char的指针视为一个字符串; if you want to print its value as a pointer you should use %p
instead. 如果要将其值作为指针打印,则应使用%p
。 For example: printf("The memory address in org is %p\\n", org);
例如: printf("The memory address in org is %p\\n", org);
From what you say I get the feeling you think that %s
will convert a number to a string. 从你的说法我觉得你认为%s
会将数字转换为字符串。 It won't do that, it expects a pointer to the first character of a string. 它不会这样做,它期望指向字符串的第一个字符。
Remember that the name of an array is equivalent to &array[0]
, ie when you pass the name of an array in a function you are passing a pointer to the first element. 请记住,数组的名称等同于&array[0]
,即当您在函数中传递数组的名称时,您将传递指向第一个元素的指针。 So here, when you call scopy(original)
it's the same as writing scopy(&original[0])
, and since original[0]
is a char
and &
is a pointer to it, that's why your function takes char *
. 所以在这里,当你调用scopy(original)
它与写scopy(&original[0])
,并且因为original[0]
是char
而且&
是指向它的指针,这就是你的函数需要char *
的原因。
Yes, you're right, when you call scopy(original);
是的,当你打电话给scopy(original);
时,你是对的scopy(original);
, scopy gets passed a pointer to the 1. element in the original
array. ,scopy传递一个指向original
数组中1.元素的指针。
scopy(original);
is the exact same as scopy(&original[0]);
与scopy(&original[0]);
完全相同scopy(&original[0]);
However, you use %s with printf. 但是,您将%s与printf一起使用。 %s expects the matching argument to be a char pointer , poiting to a sequence of chars, and the end of that sequence is a 0 byte. %s期望匹配参数是char指针,poiting到一系列字符,并且该序列的结尾是0字节。 That's what C calls a string. 这就是C所谓的字符串。
That means you can't do eg printf("%s",org[i]);
这意味着你不能做例如printf("%s",org[i]);
, since org[i] is a char, it's not a char pointer into an array that ends with a 0 byte. 因为org [i]是一个char,所以它不是指向以0字节结尾的数组的char指针。
You can print a single char by doing prinf["%c", org[0])
`while(out[i] !='\\0') 您可以通过执行prinf["%c", org[0])
`while(out [i]!='\\ 0')来打印单个字符
Or for your while loop: 或者对于你的while循环:
while(out[i] !='\0')
printf("%c",out[i]);
And you could also try: 你也可以尝试:
while(out[i] !='\0')
printf("%s",&out[i]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.