小编Phe*_*egy的帖子

(初学者)C中的指针

我被要求编写一个提供的C模块

void swap(struct posn *p, struct posn *q); 
Run Code Online (Sandbox Code Playgroud)

用于交换领域的功能ab.例:

struct posn { int x; int y; };
struct posn a = {1, 2};
struct posn b = {3, 4};
swap(&a, &b);
assert(a.x == 3);
assert(a.y == 4);
assert(b.x == 1);
assert(b.y == 2);
Run Code Online (Sandbox Code Playgroud)

然而,这两个*p,*q都是指针,所以下面的代码,我写不工作:

void swap(struct posn *p, struct posn *q)
{
    int temp1 = *p.x;
    int temp2 = *p.y;
    *p.x = *q.x;
    *p.y = *q.y;
    *q.x = temp1;
    *q.y = temp2; …
Run Code Online (Sandbox Code Playgroud)

c pointers

1
推荐指数
2
解决办法
312
查看次数

删除存储在字符串中的数字的前导零

struct number {char digits[11];};
Run Code Online (Sandbox Code Playgroud)

以下方法从(*a).digits中删除前导零

void remove_zero(struct number *a); 
Run Code Online (Sandbox Code Playgroud)

示例:(*a).digits 000013204 ---> 13204

我的方法是定义一个变量b等于(*a).digits,开始搜索b中的第一个非零数字,然后将(*a).digits替换为b的其余部分.但是,我在实现代码时遇到了麻烦

void remove_zero(struct number *a) {
char b = (*a).digits;
while (b){   // <--- (b) indicates that it hasnt reached the terminator,right?
  if (b != '0')
    { //<-- here is where to replace (*a).digits with the rest of b, but how to?
break;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

c

0
推荐指数
1
解决办法
100
查看次数

标签 统计

c ×2

pointers ×1