小编Reg*_*ser的帖子

将结构指针数组传递给函数

我正在编写一个程序,其中我必须将结构指针数组传递给主体中的函数,如下所示

     struct node *vertices[20];
create_vertices (&vertices,20);
Run Code Online (Sandbox Code Playgroud)

函数的实现是这样的

void create_vertices (struct node *vertices[20],int index)
{
}
Run Code Online (Sandbox Code Playgroud)

在此我必须传递一个索引为 20 的结构指针数组,我在 mains 之外所做的声明如下我

void create_vertices(struct node **,int);
Run Code Online (Sandbox Code Playgroud)

然而,每次编译代码时,这三行都给我带来了问题

bfs.c:26:6: error: conflicting types for ‘create_vertices’
bfs.c:8:6: note: previous declaration of ‘create_vertices’ was here
bfs.c: In function ‘create_vertices’:
bfs.c:36:15: error: incompatible types when assigning to type ‘struct node’ from type ‘struct node *’
Run Code Online (Sandbox Code Playgroud)

我无法理解我该怎么做。我希望能够做的是:

  1. 在 main 中声明一个结构指针数组(我已经这样做了)。
  2. 将数组的地址传递给函数(这是我搞砸的地方)。
  3. 在电源外声明正确的函数原型。

代码必须在 C 上,我正在 Linux 上测试它。有人可以指点我吗?

c struct pointers

4
推荐指数
1
解决办法
1万
查看次数

find命令中%p和%P之间的区别是什么

我在一个例子中遇到了如下使用find命令(它将目录结构复制到哪里)

 find olddir -name script.sh -printf "%p\0" -printf "newdir/%P\0" | xargs -0L2 cp -n
Run Code Online (Sandbox Code Playgroud)

我不清楚%p和%PI之间的差异读取了找不到多少的查找的手册页

 %p     File's name.
 %P     File's name with the name of the command line argument under which it was found removed.
Run Code Online (Sandbox Code Playgroud)

%p和%P之间有什么区别

我对它的含义感到困惑

 %P     File's name with the name of the command line argument under which it was found removed.
Run Code Online (Sandbox Code Playgroud)

bash posix find

4
推荐指数
1
解决办法
2736
查看次数

basename命令无法按预期工作

我编写了一个shell脚本,将我的大量JPG转换为pdf

 #!/bin/bash
set -xv
for i in `ls -v *.JPG`;
 do i=$(basename "$i")
 convert "$i" "$i.pdf" ;
 done
Run Code Online (Sandbox Code Playgroud)

JPG是

DSCN2612.JPG DSCN2618.JPG DSCN2624.JPG

我得到的转换后的pdf有名字

DSCN2612.JPG.pdf DSCN2618.JPG.pdf DSCN2624.JPG.pdf

现在注意在我的shell脚本中使用basename命令我希望得到pdf的结果名称

DSCN2612.pdf DSCN2618.pdf DSCN2624.pdf

输出在哪里不同.我正在使用Ubuntu 12.04和basename --version显示

basename(GNU coreutils)8.13版权所有(C)2011 Free Software Foundation,Inc.许可证GPLv3 +:GNU GPL版本3或更高版本 http://gnu.org/licenses/gpl.html.这是免费软件:您可以自由更改并重新分发它.在法律允许的范围内,不提供任何担保.

当我在一个终端上输入时

basename DSCN2612.JPG
我得到了输出

DSCN2612.JPG

我期待的地方

DSCN2612

只有这样我的理解错误或者我使用这个脚本的方式有一些错误.

unix bash shell rename ubuntu-12.04

4
推荐指数
1
解决办法
5580
查看次数

类型转换语法不清楚

我正在读一本书并遇到一个程序来读取/proc文件中的条目.他们提到的计划有以下几点

printf("%.*s", (int) n, line);
Run Code Online (Sandbox Code Playgroud)

我不清楚上面的含义

  1. 如果以上"%.*s使用什么类型的打印代替%s

代码可以在这里阅读

c printf casting

3
推荐指数
1
解决办法
131
查看次数

代码在while循环中睡觉期望输出

#include <stdio.h>
#include <unistd.h>
int main()
{
    while(1)
    {
        fprintf(stdout,"hello-out");
        fprintf(stderr,"hello-err");
        sleep(1);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我机器上面的输出是

hello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-err
Run Code Online (Sandbox Code Playgroud)

我不得不杀死程序来阻止它.这是正确和预期的行为.或者这是错误的.这是一个面试问题,因此我在这里发帖.

c

3
推荐指数
2
解决办法
1392
查看次数

printf行为

就拿int ptr={10,20,30,40,50} 我明白

print("%d", *ptr++);
Run Code Online (Sandbox Code Playgroud)

在这样的陈述中,对运营商的评估是从右到左.因此,在*ptr++++会获得第一,然后进行评估ptr,然后* 所以要确认同我写了一个程序

#include<stdio.h>
int main()
{
        int array[] = { 10, 20, 30, 40, 50 };
        int *q1 = array;
        printf("q1 = %p\n",q1);
      printf("*q1++ = %d\n",*q1++);
        printf("q1 = %p\n",q1);
      printf("*q1++ = %d\n",*q1);
}
Run Code Online (Sandbox Code Playgroud)

上述程序的输出与上述逻辑的预期运算符优先级不同.我得到的输出是

q1 = 0x7ffffcff02e0
*q1++ = 10
q1 = 0x7ffffcff02e4
*q1++ = 20
Run Code Online (Sandbox Code Playgroud)

但我在期待

q1 = 0x7ffffcff02e0
*q1++ = 20
q1 = 0x7ffffcff02e4
*q1++ = 20
Run Code Online (Sandbox Code Playgroud)

那么运营商的优先权是不是从右到左发生的?或者我的理解有些不对劲?

UPDATE

现在就是这个东西.即使我把这些括号放在所说的*(ptr ++)执行输出没有改变这里是新的代码

#include<stdio.h>
int main()
{ …
Run Code Online (Sandbox Code Playgroud)

c pointers pointer-arithmetic

3
推荐指数
2
解决办法
441
查看次数

赋值=和减法赋值 - = C中的原子操作?

int b = 1000;
b -= 20;
Run Code Online (Sandbox Code Playgroud)

上面的任何一个是原子操作吗?什么是C中的原子操作?

c atomic

3
推荐指数
1
解决办法
3633
查看次数

重命名与目录名称同名的文件

我有一个目录结构如下

dir----|
       |
       |--dir1\ some\ thing--result.pdf
       |
       |--dir2\ some\ thing--result.pdf
       |
       |--dir3\ some\ thing--result.pdf
       |
Run Code Online (Sandbox Code Playgroud)

每个子目录中的文件名是result.pdf.

目录dir1 dir2 dir3名称中有空格.我无法理解的内容(我正在编写一个bash脚本)如何在变量中获取此目录名并重命名为result.pdf

我想重命名每个文件result.pdf与目录name.pdf同名

   #!/bin/bash
    for i in *;do
    cd $i
    mv result.pd $i.pdf
    cd ..
    done
Run Code Online (Sandbox Code Playgroud)

目录名称中的空格正在产生问题.怎么过来呢?

bash shell scripting

3
推荐指数
1
解决办法
5440
查看次数

使用符号| &&〜和〜在capability.h文件中

我正在阅读这里给出的文件capability.h
我不清楚如何在函数调用中使用符号| ~ &以及&~它们在做什么

使用|以下函数调用:

static inline kernel_cap_t cap_combine(const kernel_cap_t a,
                                       const kernel_cap_t b)
{
        kernel_cap_t dest;
        CAP_BOP_ALL(dest, a, b, |);
        return dest;
}
Run Code Online (Sandbox Code Playgroud)

使用&以下系统调用:

static inline kernel_cap_t cap_intersect(const kernel_cap_t a,
                                         const kernel_cap_t b)
{
        kernel_cap_t dest;
        CAP_BOP_ALL(dest, a, b, &);
        return dest;
}
Run Code Online (Sandbox Code Playgroud)

使用&~以下功能:

static inline kernel_cap_t cap_drop(const kernel_cap_t a,
                                    const kernel_cap_t drop)
{
        kernel_cap_t dest;
        CAP_BOP_ALL(dest, a, drop, &~);
        return dest;
}
Run Code Online (Sandbox Code Playgroud)

使用~以下功能:

static …
Run Code Online (Sandbox Code Playgroud)

c posix linux-kernel embedded-linux

3
推荐指数
1
解决办法
87
查看次数

删除给定子目录中的文件

我在给定文件夹中有几个子目录,其中存在d2.sh~文件.我想通过以下shell脚本删除这个文件,而不是写在我在终端上写的.sh文件中的一行.[ 编辑:为了清晰起见,此处已正确格式化]

for i in `ls *`; do
    if [ -d $i ]; then
        cd $i
        rm d2.sh~
        cd ..
    fi
done
Run Code Online (Sandbox Code Playgroud)

这没有给我任何错误,但它无法d2.sh~从子目录中删除.所以我想知道我上面犯了什么错误?

bash shell

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