小编BLU*_*IXY的帖子

列表初始化带有2个char []指针的向量<string>

我正在从命令行运行C++ 11程序,如此

marco @ host $ ./program.out a bb ccc

用于生成可执行文件的.cpp文件如下所示:

#include<iostream>
#include<vector>
#include<string>

int main(int argc, char* argv[]){
  std::vector<std::string> strs = {argv + 1, argv + argc};
  std::cout << strs[2] << endl;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我运行它时,它输出ccc.我没想到,因为列表初始化程序只包含char*类型的两个元素.我之前看过这样的列表内容:

vector<string> strs = {"str1", "str2", ..., "strN"};
Run Code Online (Sandbox Code Playgroud)

但指针init对我来说是全新的.似乎指针被视为开始和结束迭代器.我的问题是,编译器如何知道从argv + 1迭代到argv + argc来初始化strs中的各个字符串?只是为了把事情放在上下文中,有没有办法在C中以类似的方式使用列表初始化来初始化char*数组?

c++ pointers

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

是否可以确定字符串是否为"只读"?

是否有可能确定字符串是否不能写在传递给它的那一边char * string

我的代码:

#include <stdio.h>

typedef enum string_type 
    { READ_ONLY, READ_WRITE }
    String_type;

String_type isvalid(char *s);

void test(char *s){
    if(isvalid(s))
        printf("OK\n");
    else
        printf("NG\n");
}

int main(void){
    char data_str[] = "data_str";

    test("data_str");// fails
    test(data_str);// works

    return 0;
}

String_type isvalid(char *s){
    //Can it be determined by this?
    //I don't think this is a portable way.
    return (void *)s > (void *)main ? READ_ONLY : READ_WRITE;
}
Run Code Online (Sandbox Code Playgroud)

c

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

计算文件中的行数,不包括 C 中的空行

我们有一个程序,它将一个文件作为输入,然后计算该文件中的行数,但不计算空行数。

Stack Overflow 中已经有一篇关于这个问题的帖子,但这个问题的答案并没有涵盖我。

我们举一个简单的例子。

文件:

I am John\n
I am 22 years old\n
I live in England\n
Run Code Online (Sandbox Code Playgroud)

如果最后一个 '\n' 不存在,那么计数会很容易。我们实际上已经有一个函数可以做到这一点:

/* Reads a file and returns the number of lines in this file. */
uint32_t countLines(FILE *file) {
  uint32_t lines = 0;
  int32_t c;
  while (EOF != (c = fgetc(file))) {
    if (c == '\n') {
      ++lines;
    }
  }
  /* Reset the file pointer to the start of the file */
  rewind(file);
  return lines;
}
Run Code Online (Sandbox Code Playgroud)

这个函数,当把上面的文件作为输入时,计算了 4 行。但我只想要 3 …

c lines count

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

二维字符数组

在输出中,不打印最后一个字符.

Input: 3 3
       abcabcabc
Expected Output: a b c a b c a b c
Actual Output: a b c a b c a b
Run Code Online (Sandbox Code Playgroud)

c ???在哪里?

#include <stdio.h>
int main() {
    int i,j,k,n;
    char a[3][3],b[3][3];
    printf("enter size\n");
    scanf("%d %d",&n,&k);
    printf("enter character \n");
    for(i=0;i<n;i++)
        for(j=0;j<k;j++)
            scanf("%c",&a[i][j]);
    printf("\n");
    for(i=0;i<n;i++)
        for(j=0;j<k;j++)
            printf("%c ",a[i][j]);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c arrays character

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

++*p ++无法理解优先级

运行此代码:

#include <stdio.h>

int main() {
    int x[]={20,30};
    int *p=x;
    ++*p++;
    printf("%d %d\n",x[0],*p);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是21 30,这对我来说没有意义,因为根据C运算符优先级,后缀增量首先出现,但如果在我看来输出应该是20的情况.对于记录我是新的编程,它真的似乎我不能得到它如此抱歉如果这个问题是愚蠢的:)

c pointers operator-precedence

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

如何使用fnmatch

我正在尝试使用fnmatch函数fnmatch.h

printf("match: %i") ,fnmatch("hello", "hello world", 0);

以上打印出一个较大的负数.电话怎么样?

c linux

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

需要一步一步地解释"while((ch = getchar())!='\n'&& c!= EOF;"

我知道这个问题已被提出; 但它没有一步一步地解释,或者彻底解释它是如何执行的; 所以,假设我有这部分代码:

char ch;
while((ch = getchar()) != '\n' && ch != EOF);
Run Code Online (Sandbox Code Playgroud)

getchar()是否将单个字符读入ch变量1st,然后进行比较,即ch != '\n' && ch != EOF保留换行转义序列,但实际上不是ch变量?如果是这样,这是不是意味着它会无限循环直到它遇到换行转义序列/ EOF?如果遇到换行转义序列,它是否存储在ch变量中?如果没有,怎么回事?

c stdin getchar

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

在C语言中,我可以通过堆栈指针访问另一个函数中的main函数的局部变量吗?

我需要访问main函数中定义的变量a的值,而不将其作为参数传递.

main()
{
    int a=10;

    func();

    printf("%d\n",a);
}

void func(){
//i need access of variable a here.
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

c stack gcc stack-pointer

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

结构数组的指针初始化

我的应用程序在崩溃时崩溃了.

所以我有一个像这样的结构(但实际上它有更多的东西)

struct Record
{
    float m_fSimulationTime;
    unsigned char m_szflags;
};
Run Code Online (Sandbox Code Playgroud)

在我的课堂上,我声明它是这样的:

Record *m_record[64];
Run Code Online (Sandbox Code Playgroud)

然后我将其初始化:(并且此处崩溃发生(读取时的访问冲突))

void ClassXYZ::initRecord()
{
    for (int i = 0; i <= 32; i++)
        for (int j = 0; j < 9; j++)
            m_record[i][j].m_fSimulationTime = 0.0f; // here happens the crash
}
Run Code Online (Sandbox Code Playgroud)

我希望你能帮我解决我在这里缺少的xx

谢谢你的建议!

c++ struct

-1
推荐指数
1
解决办法
58
查看次数

一维数组是作为字符串列表还是单个字符串工作?

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

int main()
{
    int i;
    char a[10];
    for(i=0;i<10;i++)
    {
        scanf("%s",a);// >how this line is working in memory.
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我想知道字符串是如何保存在内存中的,因为我已经将它初始化为1D字符数组,但是数组是作为字符串列表还是单个字符串工作?为什么?

c

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