小编too*_*ite的帖子

C/C++ 中的 float* 和 float[n] 有什么区别

使用有什么区别

float* f;
Run Code Online (Sandbox Code Playgroud)

float f[4];
Run Code Online (Sandbox Code Playgroud)

或者他们的处理方式完全相同?使用一个而不是另一个可能会导致您遇到内存分配问题吗?如果没有,是否有任何情况下他们被区别对待?

不知道这是否相关,但我使用类型 float* 作为函数参数。例如:

void myfun(float* f){
     f[0] = 0;
     f[1] = 1;
     f[2] = 2;
     f[3] = 3;
}
Run Code Online (Sandbox Code Playgroud)

它编译并运行良好(我不确定为什么 - 我认为因为我没有分配任何内存f,它会抛出某种异常)。

c pointers static-memory-allocation

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

以下代码有什么问题?

我正在自己学习C,但这段代码对我来说似乎不对

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
main()
{
    char name[20];
    int p,c,m;
    printf("Enter your name \n");
    scanf(" %s", name);
    if ( (name=='luv') || (name='pranav') )
    {
        printf("Enter your marks in pcm \n");
    }
    else
    {
        printf("get lost");
    }
    getch();
}
Run Code Online (Sandbox Code Playgroud)

我想,如果我输入的名称正确的代码只运行luv或者pranav而是正在发生的事情是,无论什么名字我键入它运行在其他的代码,我无法找出原因.

我使用代码块作为编译器.

c

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

检查字符串是否只包含数字?(需要isDigitMethod)

用户应该给出一个字符串,我们应该检查所有字符是否都是数字.如果所有都是数字,我们将收到一条密码为数字的消息,然后继续执行该程序.如果其中任何一个(或全部)不是数字,则用户应再次输入密码.(例如465486可以接受.hell3429384或者sfdkjsfkj是不可接受的)

显然,我的代码工作不正常,因为无论是给密码输入数字还是字母,我都会得到相同的结果.我是在循环错误,还是我使用isDigit错误的方法?

import java.util.*; 
public class Password {
    public static void main(String[] args) {
        Scanner passn = new Scanner(System.in);
        System.out.println("Give password: "); 
        String pass = passn.nextLine(); /* user enters password*/
        int length = pass.length();
        boolean isadigit; 
        for(int i = 0; i < length; i++) { /*looping each character one by    one*/     
            char character= pass.charAt(i);
            if (Character.isDigit(i)) { /*checking if each character is digit*/
                isadigit = true; /* if it is, …
Run Code Online (Sandbox Code Playgroud)

java

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

为什么我在以下代码中遇到分段错误(核心转储)?

在这个代码中使用 a--b++显示分段错误,但如果给予--a++b它的工作,为什么?!

add(a,b)
{ 
   if (a==0)
       return b;
    else
       return add(a--,b++); //why this line didn't work??! 
}
Run Code Online (Sandbox Code Playgroud)

c recursion segmentation-fault post-increment

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

C/C++ 未初始化数组的结果

这可能是一个无聊的问题!谢谢!

这是代码:

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
        int a[5] = {0};
        int b[5];
        cout << a << endl;
        cout << b << endl;
        for (int i = 0; i < 5; i++)
        {
                cout << a[i] << " ";
        }
        cout << endl;
        for (int i = 0; i < 5; i++)
        {
                cout << b[i] << " ";
        }
        cout << endl;
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

在 Ubuntu 中:g++ a.cpp

在此处输入图片说明

在带有 DEV C++、MinGW GCC 4.7.2 …

c++ arrays c++11

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

C++中的C风格转换给出了奇怪的行为

我总是读到C++中的C风格演员与reinterpret_cast相同.但是,我刚刚在Visual Studio中测试了这段代码,看起来C样式的转换与静态转换的行为相同.有人知道为什么吗?这似乎是一个错误......

#include <string>
#include <iostream>

class Thing1
{
    std::string theString;

public:
    Thing1(const std::string& theString) : theString(theString)
    {
        //
    }

    std::string getTheString()
    {
        return theString;
    }
};

class Thing2
{
    std::string theString;

public:
    Thing2(const std::string& theString) : theString(theString)
    {
        //
    }

    std::string getTheString()
    {
        return theString;
    }
};

class Thing3 : public Thing1, public Thing2
{
public:
    Thing3(const std::string& theString1, const std::string& theString2) : Thing1(theString1), Thing2(theString2)
    {
        //
    }
};

int main()
{
    Thing3* thing3 = new Thing3("string1", "string2");
    uintptr_t …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance casting multiple-inheritance

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

如何在Arduino中进行有限循环

我是编码的初学者,尤其是Arduino.我一直在做很多项目,但我遇到了一个问题.我似乎无法弄清楚如何进行有限循环.我正在看一个类似于while循环的东西,它在四次后停止.这里是实施的地方,让您更好地了解我在寻找什么.

#include <Servo.h>

int thumbPin = 2;
int ndxPin = 3;
int midPin = 4;
int rngPin = 5;
int pnkyPin = 6;

Servo thumb;
Servo index;
Servo middle;
Servo ring;
Servo pinky;
void setup() {
  Serial.begin(9600);

  thumb.attach(thumbPin);
  index.attach(ndxPin);
  middle.attach(midPin);
  ring.attach(rngPin);
  pinky.attach(pnkyPin);
}
void loop() {
  /* I want this code in the comment to be ran four times, then continued on to the code after
  thumb.write(0);
  delay(20);
  thumb.write(0);
  index.write(0);
  middle.write(0);
  ring.write(0);
  pinky.write(0);

  thumb.write(150);
  index.write(150);
  middle.write(150);
  ring.write(150);
  pinky.write(150); …
Run Code Online (Sandbox Code Playgroud)

arduino arduino-uno

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

为什么编译时不会抛出错误?

#include <stdio.h>

int main()
{
  int i = 0;
  int *a;
  do
    {
      scanf("%d",&a[i]);
      i++;
    }
  while (a[i-1]!=-1);
}
Run Code Online (Sandbox Code Playgroud)

此代码读取数字直到-1遇到.指针a未初始化,但代码仍然编译并运行,没有任何错误.为什么?这可以是一种初始化数组而不声明其大小的方法吗?

c pointers

-9
推荐指数
2
解决办法
93
查看次数

为什么C/C++比Assembly和其他低级语言慢?

我编写了一个代码,在C++中什么都不做

void main(void){

}
Run Code Online (Sandbox Code Playgroud)

和大会.

.global _start
.text

_start:
    mov $60, %rax
    xor %rdi, %rdi 
    syscall
Run Code Online (Sandbox Code Playgroud)

我编译C代码并编译和链接汇编代码.我用time命令比较了两个可执行文件.

部件

time ./Assembly

real    0m0.001s
user    0m0.000s
sys     0m0.000s
Run Code Online (Sandbox Code Playgroud)

C

time ./C

real    0m0.002s
user    0m0.000s
sys     0m0.000s
Run Code Online (Sandbox Code Playgroud)

汇编速度比C快两倍.我反汇编代码,在汇编代码中,只有四行代码(相同).在C代码中,有大量不必要的代码用于将main链接到_start.主要有四行代码,其中三行代表不可能(你不能从函数博客外部访问函数的变量)从' block ' 外部访问' local '(如函数变量)变量(像功能块一样).

push %rbp ; push base pointer.
mov  %rsp, %rbp ; copy value of stack pointer to base pointer, stack pointer is using for saving variables.
pop  %rbp ; 'local' variables are removed, because we pop the base …
Run Code Online (Sandbox Code Playgroud)

c++ assembly

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