标签: initialization

未初始化的指针vs NULL和0

我有一个未初始化的int指针.打印时,此值始终显示为0.

但是当检查NULL时,它不会通过条件.

有趣的是,这个野生指针在没有抛出Segmentation Fault的情况下获取价值.

任何解释??

代码如下:

int main (){

int *p1;
int *p2;
int var=90;
printf("p1 = %x\n", p1);
printf("p2 = %x\n", p2);

p1 = &var;

if(p2==NULL)
{return 0;}

*p2 = *p1;

printf("*p2 = %x\n", *p2);

}
Run Code Online (Sandbox Code Playgroud)

输出看起来像这样..

# gcc -std=c99 -o main *.c
# main
p1 = 0
p2 = 0
*p2 = 90
Run Code Online (Sandbox Code Playgroud)

c pointers initialization

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

为什么隐式类型的局部变量必须在C#中而不是在JavaScript中初始化?

if (i==101)
    {
        var lastLoopCheck;
    }
Run Code Online (Sandbox Code Playgroud)

Visual Studio 2013显示错误: - "必须初始化隐式类型的局部变量"

为什么?这是什么原因?

我的第一语言是JavaScript,其中允许未初始化的变量(本地和全局); 看看下面的(JavaScript)代码:

var foo;
alert(typeof foo); //undefined
Run Code Online (Sandbox Code Playgroud)

那么为什么在JavaScript中而不是在C#中允许使用未初始化的变量?

javascript c# variables initialization

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

初始化一个静态const std :: pair <string,vector <string >>

我有一个

class myclass
{
    // ...
    static const vector<pair<string,vector<string>>> var;
    // ...
};
Run Code Online (Sandbox Code Playgroud)

在类定义中,使用单个字符串到其他几个字符串的映射.我使用vector <>而不是数组,以便能够添加映射对和映射长度,而不必使用大小变量.有没有办法在相应的.cpp文件中初始化变量,就像非复合类型的向量一样,即格式为:

 const vector<pair<string,vector<string>>> myclass :: var =
{
   ???
}
Run Code Online (Sandbox Code Playgroud)

或者我必须使用静态方法,如

static myclass::initStaticMembers(){...}
Run Code Online (Sandbox Code Playgroud)

如果有第一种方法可以做到这一点,那么语法是什么?我搜索过,但没有找到复合std :: pair初始化的语法.例如,你可以初始化一个vector<string>

vector <string>myvec={"elem1", "elem2","elem3"};
Run Code Online (Sandbox Code Playgroud)

但你怎么开始复杂的vector<pair<string,vector<string>>>?谢谢.

c++ initialization composite stdvector std-pair

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

变量可能尚未初始化

对于我的Comp Sci课程,我必须创建一个程序来查找给定月份(1-12)中的天数,你知道为什么我在尝试时遇到错误"变量天数可能没有被初始化"从交换机返回int"days"?这是代码:

public static int getNumberofDays(int month,int year)
   {
      // Imports the required Scanner
      Scanner kbd = new Scanner(System.in);

      final String month;
      final int days;
      switch (month) {
         case 1:  days = 31;
            break;
         case 2:  if ((year % 4 == 0) && year % 100 != 0)
                  {
                     days = 29;
                  }
                  else if ((year % 4 == 0) && (year % 100 == 0) && (year % 400 == 0))
                  {
                     days = 29;
                  }
                  else
                  {
                     days …
Run Code Online (Sandbox Code Playgroud)

java variables initialization

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

如何在C#中将csv导入List <object>

我正在使用下面的类创建一个List对象:

public class Item
{
    public string A { get; set; }
    public string B { get; set; }
    public string C { get; set; }
    public string D { get; set; }
    public int E { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

像这样:

public List<Item> Flower = new List<Item>{};
Run Code Online (Sandbox Code Playgroud)

我如何使用结构如此的csv文件的内容初始化此列表?

A,B,C,D,1
Run Code Online (Sandbox Code Playgroud)

c# csv initialization list initializer-list

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

如何在C++中声明后初始化一个不同大小的std :: vector?

我需要做以下事情:

vector<int> v;
int flag = 0;

if (flag) {
// initialize v with size 100;
} else {
// initialize v with size 0;
}
...
if (flag) {
// do something with v, given flag != 0
} else {
// don't do with v.
}
Run Code Online (Sandbox Code Playgroud)

这样做的正确方法是什么?谢谢!

c++ initialization vector

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

为什么我不能使用'0x80000000'来初始化int数组?

#include<iostream>
int main()
{
    int arr[1] = {0x80000000};
    std::cout<<arr[0]<<"\n";
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

上面的代码将收到以下错误:

错误:缩小'2147483648u'从'unsigned int'到'int'的转换{} [-Wnarrowing] int arr [1] = {0x80000000};

但以下代码完美运行:

#include<iostream>    
int main()
{
    int arr[1];
    arr[0] = 0x80000000;
    std::cout<<arr[0]<<"\n";
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

所以我的问题是:为什么我不能使用'0x80000000'初始化一个数组int

c++ arrays int initialization list-initialization

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

数组元素(在使用复合赋值运算符之后)产生垃圾值,为什么?

#include<stdio.h>
void main(void)
{
    int a[5]={90,78,77,98,98}, b[5]={80,45,67,88,57}, c[5]={88,99,65,55,74},total[3],i,j;
    for(j=0;j<=4;j++)
        {
            total[0]+=a[j];
            total[1]+=b[j];
            total[2]+=c[j];
        }

    for(i=1;i<=3;i++)
    {
    printf("%d?? ?? ? : %d\n",i,total[i-1]);
    }
}
Run Code Online (Sandbox Code Playgroud)

total[0]并且total[1]是正确的价值,但是total[2]错误的价值.我找不到自己的错.你能解释一下吗?

c arrays initialization compound-assignment

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

CppUnit:为什么静态局部变量保持其值?

我正在尝试使用CppUnit来测试一个只在第一次调用时才执行某些代码的方法.

class CElementParseInputTests: public CppUnit::TestFixture {
private:
    CElement* element;
public:

    void setUp() {
        element = new CElement();
    }

    void tearDown() {
        delete element;
    }

    void test1() {
        unsigned int parsePosition = 0;
        CPPUNIT_ASSERT_EQUAL(false, element->parseInput("fäil", parsePosition));
    }

    void test2() {
        unsigned int parsePosition = 0;
        CPPUNIT_ASSERT_EQUAL(false, element->parseInput("pass", parsePosition));
    }
Run Code Online (Sandbox Code Playgroud)

我想测试的递归方法:

bool CElement::parseInput(const std::string& input, unsigned int& parsePosition) {
    static bool checkedForNonASCII = false;
    if(!checkedForNonASCII) {
        std::cout << "this should be printed once for every test case" << std::endl;
        [...]
        checkedForNonASCII = …
Run Code Online (Sandbox Code Playgroud)

c++ oop static cppunit initialization

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

如何初始化vector <char>的向量?

我有来自数独拼图的数据.我必须在下面定义一些内容vector<vector<char>>.

[[".", "8", "7", "6", "5", "4", "3", "2", "1"], 
 ["2", ".", ".", ".", ".", ".", ".", ".", "."],
 ["3", ".", ".", ".", ".", ".", ".", ".", "."], 
 ["4", ".", ".", ".", ".", ".", ".", ".", "."],
 ["5", ".", ".", ".", ".", ".", ".", ".", "."], 
 ["6", ".", ".", ".", ".", ".", ".", ".", "."], 
 ["7", ".", ".", ".", ".", ".", ".", ".", "."], 
 ["8", ".", ".", ".", ".", ".", ".", ".", "."], 
 ["9", ".", ".", ".", ".", …
Run Code Online (Sandbox Code Playgroud)

c++ initialization vector

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