【问题标题】:Obtain the first element from an array variable that is passed by reference从通过引用传递的数组变量中获取第一个元素
【发布时间】:2020-05-28 10:09:00
【问题描述】:

我无法通过引用数组和指针来获取单个元素信息。 此外,我无法重写指针值并将其传递回主引用。

任何帮助将不胜感激!

void strings(char word[][40], char *first, char *last, int size);

int main()
{
 char word[10][40];
 char first[40], last[40];
 int i, size;

 printf("Enter size: \n");
 scanf("%d", &size); //storing size
 printf("Enter %d words: \n", size);
 for (i=0; i<size; i++)
 scanf("%s", word[i]); //storing user input
 deriving(word, first, last, size);
 printf("First word = %s, Last word = %s\n", first, last);
 return 0;
}
void deriving(char word[][40], char *first, char *last, int size)
{
    int count = 0;
    char *ptr = word; //why is it when i store the user input as a  ptr variable i am able to get each element?
    while(*ptr != '\0')
    {
        printf("number of lines = number of elements \n");
        ptr++;
    }

    while(*(word+count) != '\0')
    {
        count ++;
        printf(" element is present");  // it is stuck in a infinite loop here. 
    }

    first = "first word"; //why does this two commands not carry into the main function? since i am rewriting the whole pointer
    last = "last word";

}

【问题讨论】:

  • 请记住,C 中的参数是按值传递的。这意味着值被复制到参数变量中。修改副本(例如分配给参数变量)只会修改副本,而不是原始副本。您可能想了解在 C 中模拟通过引用传递,或者将指针传递给数组的第一个元素,然后将其strcpy 传递给。

标签: c arrays pointers multidimensional-array c-strings


【解决方案1】:

首先声明此声明

char *ptr = word;

无效。根据参数word 的声明,初始化器的类型为char ( * )[40] 类似

char word[][40]

并且没有从一种指针类型到另一种指针类型的隐式转换。

所以你必须写

char ( *ptr ) = word;

结果是这些循环

while(*ptr != '\0')
{
    printf("number of lines = number of elements \n");
    ptr++;
}

while(*(word+count) != '\0')
{
    count ++;
    printf(" element is present");  // it is stuck in a infinite loop here. 
}

也不正确,因为数组字没有初始化。

char word[10][40];

考虑到变量word 的类型为char ( * )[40],然后取消引用表达式*(word+count),您将得到一个char[40] 类型的对象。将它与值'\0' 进行比较是没有意义的,因为这种比较相当于

*(word+count) != NULL

由于左操作数不是空指针,因此条件计算为真。

对于firstlast,它们是函数的局部变量,具有传递参数值的副本,即它们包含用作函数参数的数组的第一个字符的地址。所以改变局部变量不会影响原来的数组。

此外,您不能重新分配数组,因为数组是不可修改的左值。

您必须将字符串文字复制到数组的元素中。

例如

strcpy(first, "first word" ); 
strcpy( last, "last word" );

这是一个演示程序

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

#define N   40

void f( char *first, char *last )
{
    strcpy( first, "first word" ); 
    strcpy( last, "last word" );
}

int main(void) 
{
    char first[N], last[N];

    f( first, last );

    printf( "First word = %s, Last word = %s\n", first, last );

    return 0;
}

它的输出是

First word = first word, Last word = last word
enter code here

还有一个演示程序

#include <stdio.h>

#define M   10
#define N   40

void f( char word[][N], size_t n )
{
    size_t count = 0;
    char ( *p )[N] = word;

    while ( count < n && **( p + count ) != '\0' ) ++count;

    printf( "There are %zu non-empty strings\n", count );
}

int main(void) 
{
    char word[M][N] =
    {
        "first", "second", "third"
    };

    f( word, M );

    return 0;
}

它的输出是

There are 3 non-empty strings

函数f是正确的,因为数组字被初始化了。

【讨论】:

  • 谢谢! strcpy 命令现在对我有用!在 while(*(word+count) != '\0') { count ++; printf("元素存在"); // 这里陷入了无限循环。 } 你如何建议我使用这种方法来寻找循环的结尾?使用指针,它是一个以 \0 结尾的字符串,因此我也将它用于这部分。 @vlad
猜你喜欢
  • 2017-04-18
  • 2016-04-09
  • 2016-04-10
  • 2021-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-06
相关资源
最近更新 更多