C 使用指针初始化字符数组
这个问题快把我逼疯了,我肯定我错过了什么。我只需要使用指针初始化一个字符数组。以下是我目前掌握的代码:C 使用指针初始化字符数组,c,char,C,Char,这个问题快把我逼疯了,我肯定我错过了什么。我只需要使用指针初始化一个字符数组。以下是我目前掌握的代码: int p2(){ /* Implements problem 2 of lab */ // Create an array char **s = (char**)malloc( 11 *sizeof(char)); char *p = *s; char start ='A';
int p2(){
/* Implements problem 2 of lab */
// Create an array
char **s = (char**)malloc( 11 *sizeof(char));
char *p = *s;
char start ='A';
while( p != s+10){
*p = start;
start++;
p++;
}
return(0);
}
我遇到的问题是我不知道如何处理数组中的字符。我知道数组的基址是**s,指向第一个元素的指针是*s。我不明白的是如何到达**s+10(即数组的末尾)
谁能帮我照点光吗???求你了
编辑:好吧,看来我误解了这个问题。我似乎需要创建一个字符串数组(因此是char**分配)。然后我需要循环这个数组,并为每个字符串(即char*)分配一个15个字符长的值。如果我理解正确,请告诉我:
char**strings==>strings[0…n],其中每个元素都是指向char(可能是数组)的指针。对于*string==>strings[0]、*(string+1)=strings[1]等,都有一个表达式
我离得近还是远 我想你的意思是:
char *s = (char*) malloc(11 *sizeof(char));
char *p = s;
在这种情况下,首先用
s[x]
对字符进行寻址,除非需要数组数组,否则不需要char**
其次,您可以使用(*s)[10]
或*(*s)+10)到达数组的末尾
第三,在C编程中,不要强制转换malloc()的结果。
这里的代码正在崩溃
char **s = (char**)malloc( 11 *sizeof(char));
您为11个char
s分配了足够的内存,这听起来像是您想要做的
但是,您正在将这11个
char
s的地址强制转换为(char**)
,就好像您正在为指针分配空间,而不是char
s。char**s
是二维字符数组,或者是C字符串数组(如果需要)。
如果要使用字符数组,应使用:
char *string = (char*)malloc( 11 *sizeof(char));
for( int i = 0; i < 11; i++){
char *p = *(s+i);
for( int j = 0; j < 10; j++){
*(p + j) = 'a'; // Say you wanna fill them with as
}
// You may also use this syntax:
while( p < (*(s+i) + 10)){ // or use != instead of <
*p = 'a';
p++;
}
}
如果确实要初始化字符串数组,则在第一步初始化指针数组,即:
char **s = (char**)malloc( 11 *sizeof(char *));
请注意,我在sizeof中使用了char*
。而不是使用字符串,但首先必须初始化每个字符串
s[0] = (char*) malloc( 15*size(char)); // This is actually string, 14 characters long (NULL terminated)
char *p = s[0]; // p is pointer to beginning of your string now
有两种方法可以寻址字符串:
s[0][3] // 4th character of your string
p[3]
或者,如果您只想使用指针:
char *p = *(s+0);
*(p+3); // 4th character
*((*(s+0))+3) // To do it "hardcore"
编辑:添加了一个示例
当您使用**char p
并使用p++
或p+1
时,C会增加内存地址*p
运算符告诉编译器,您现在希望使用存储在内存中的数据,而不是指针。因此,这两种语法的作用相同:
p[10] = 'a';
*(p+10) = 'a';
因此,如果要遍历两个维度,应使用:
char *string = (char*)malloc( 11 *sizeof(char));
for( int i = 0; i < 11; i++){
char *p = *(s+i);
for( int j = 0; j < 10; j++){
*(p + j) = 'a'; // Say you wanna fill them with as
}
// You may also use this syntax:
while( p < (*(s+i) + 10)){ // or use != instead of <
*p = 'a';
p++;
}
}
for(int i=0;i<11;i++){
char*p=*(s+i);
对于(int j=0;j<10;j++){
*(p+j)='a';//说你想用as填充它们
}
//您也可以使用以下语法:
而(p<(*(s+i)+10)){//或使用!=代替<
*p=‘a’;
p++;
}
}
为什么要使用双指针创建字符数组
char *s = (char *) malloc(10 * sizeof(char));
char *start = s; // save starting pointer
for(; s < start+10; s++)
*s = 'a';
return 0;
char*s=(char*)malloc(10*sizeof(char));
char*start=s;//保存起始指针
对于(;s
如果分配char**本质上就是分配一个指向char的指针数组(例如字符串数组)。如果需要字符数组,请分配字符数组。如果您开始使用指针,堆栈和堆的设计将非常有用。是的,我需要为10个字符指针(字符数组,又称字符串)分配空间。然后我需要在数组中循环,并为指针赋值。如何处理我在**s中创建的指针?@certifiedNoob:一个字符数组/字符串不需要10个指向它的指针。一个人来做这项工作。这就是马洛克回来的原因。指向数组开头的一个指针。那么如何遍历数组(**s?)。会是pCurr=*s;虽然(pCurr!=*(s+10)){…pCurr++}@certifiedNoob我已经添加了遍历示例,希望它们对您有所帮助…感谢您文章中的第三行。不幸的是,每一个其他的答案都会带来
malloc