问题列表 - 第16824页

奇怪的结果将指针打印为C中的float

我知道这是错的,gcc会给你一个关于它的警告,但为什么它会起作用(即数字打印正确,有一些舍入差异)?

int main() {
   float *f = (float*) malloc(sizeof(float));
   *f = 123.456;
   printf("%f\n", *f);
   printf("%f\n", f);
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

编辑: 是的,我正在使用带有32位机器的gcc.我很想知道其他编译器会得到什么结果.

在Christoph的建议之后,我更多地干涉了一些事情:

int main() {
   float *f = (float*) malloc(sizeof(float));
   *f = 123.456;
   printf("%f\n", f); // this
   printf("%f\n", *f);
   printf("%f\n", f); // that
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

这导致第一次printf打印的值与最后的printf不同,尽管是相同的.

c floating-point printf gcc pointers

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

C中存在冲突类型错误

对于以下C代码(用于交换两个数字),我得到swap函数的"冲突类型"错误.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a,b;
    printf("enter the numbers to be swapped");
    scanf("%d%d",&a,&b);
    printf("before swap");
    printf("a=%d,b=%d",a,b);
    swap(&a,&b,sizeof(int));
    printf("after swap");
    printf("a=%d,b=%d",a,b);
    getch();   
}
void swap(void *p1,void *p2,int size)
{
     char buffer[size];
     memcpy(buffer,p1,size);
     memcpy(p1,p2,size);
     memcpy(p2,buffer,size);
     return(0);
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以告诉为什么会出现错误?
那是什么解决方案?

c

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

抽象在这种情况下意味着什么?

我需要一些帮助来理解python概念.

class TilePuzzleProblem(search.Problem):
""" This class is the class for the NxN - blanks tile puzzle problem """

    def __init__(self, N, blanks, initial, goal):
        """ Initialize """
        search.Problem.__init__(self, initial, goal)
        self.N = N
        self.blanks = blanks

    def successor(self, state):
        """ Generate the successors of the given state. Returns a list of (move, successor) pairs"""
        abstract

    def h(self, node):
        abstract
Run Code Online (Sandbox Code Playgroud)

目前代码挂在abstract函数的一部分h(...),但我不知道是什么abstract意思,因此无法理解问题是什么.

python abstract

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

Python元类与类装饰器

Python元类和类装饰器之间的主要区别是什么?有什么我可以用一个而不是另一个吗?

python metaclass decorator

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

无法检测到导致语法错误的行

你好!我正在从我的一个编程课程开始一个旧项目,并且有一些问题需要跟踪我的代码中的哪一行导致语法错误.

当我尝试编译代码时,Visual Studio告诉我在包含此函数调用的行的main末尾有一个语法错误:

sortData(carray,numRec,sortby);

我不认为是这种情况,因为注释掉该调用只会将错误移动到下一行代码.

我不知道是什么导致了这个错误,我希望老手的眼睛可以提供帮助.

我已经包含了所有代码,因为我怀疑在其中一个函数调用中导致了错误.

ohbijuan

PS此外 - 有没有人知道编译器是从上到下编译还是在编译过程中它是否实际跟随函数调用?

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

//customer struct
struct customer
{
    int id;
    string fname, lname;
    double totalQuantity, totalPurchases, totalProfit;
};

//Error Codes
const int INPUT_FILE_FAIL_NO_CONTINUE = 2;
const int INPUT_FILE_DATA_ERROR = 3;

//Global Constants
const int arraySize = 200;
const int numType = 5; //# of the types of coffee we currently offer

//Coffee prices per pound
const double colombWhole = 3.75;
const …
Run Code Online (Sandbox Code Playgroud)

c++ syntax

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

C#设计模式 - 如何根据高度可配置的用户选择编写代码

我想编写没有很多开关的代码,if/else,以及其他基于用户输入执行逻辑的典型语句.

例如,假设我有一个我要组装的Car类并调用Car.Run().更重要的是,让我们说轮胎我有4个不同轮胎类的chocie可供选择,根据用户输入.

对于,我不知道,身体类型,让我说我有10个体​​型类可供选择来构建我的汽车对象,依此类推.

使用可配置参数的数量将此示例放大1000时使用的最佳模式是什么.

甚至有这种模式吗?我看过工厂和抽象的工厂模式,他们不太适合这个,虽然看起来应该这样.

c# design-patterns

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

flash [:notice]在Rails中不起作用

我的控制器中有以下代码片段

  def create
    @message = Message.new(params[:message])
    @message.message = h(@message.message)
    if @message.save
       flash[:message] = "Message Sent. Thank You for Contacting Me"
    else
       flash[:message] = "OOps Something went wrong"
    end
    redirect_to :action => 'contact'
  end
Run Code Online (Sandbox Code Playgroud)

当我尝试在联系表单中显示flash消息时,它不会显示.我查找了可能的解决方案,但它们似乎不起作用.出了什么问题?

ruby-on-rails

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

DataSnap的大流

我试图在DataSnap服务器/客户端之间传输一些大流(~1Mb),但无济于事.我试图理解吉姆蒂尔尼的代码(http://blogs.embarcadero.com/jimtierney/2009/04/06/31461)没有运气,我甚至无法编译代码,因为缺少库,无论如何......

我能够接收的流的最大大小是64k,因此您可以为像我这样的周末程序员提供的任何提示/想法/代码示例都会受到欢迎.谢谢!

我的服务器代码:

function TsrvMethods.getStream(iCount: integer): TStream;
begin
  Result := dummyStream('0123456789', iCount);
end;

function dummyStream(sCnt: string; iCount: integer): TStream;
begin
  Result := TMemoryStream.Create;
  while iCount > 1 do begin
    Result.Write(Pointer(sCnt)^, Length(sCnt));
    Dec(iCount);
  end;
  Result.Seek(0, TSeekOrigin.soBeginning);
end;
Run Code Online (Sandbox Code Playgroud)

我的客户端调用代码:

procedure TfrmMain.butStreamClick(Sender: TObject);
var
  sStr : TStream;
begin
  cycleConnection; //make sure we have an active connection

  with TsrvMethodsClient.Create( SQLConn.DBXConnection, False ) do begin
    sStr := getStream( Integer(SpinCount.Value) );
    Free;
  end;
  FreeAndNil(sStr);
end;
Run Code Online (Sandbox Code Playgroud)

delphi stream datasnap

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

HttpCookie和Cookie之间的区别?

所以我很困惑,因为msdn和其他教程告诉我使用HttpCookies通过Response.Cookies.Add(cookie)添加cookie.但这就是问题所在.Response.Cookies.Add只接受Cookies而不接受HttpCookies,我收到此错误:

无法从'System.Net.CookieContainer'转换为'System.Net.Cookie'

另外,Response.Cookies.Add(cookie)和Request.CookieContainer.Add(cookie)之间有什么区别?

感谢您的帮助,我正在尝试使用C#自学.

// Cookie
Cookie MyCookie = new Cookie();
MyCookie.Name = "sid";
MyCookie.Value = SID;
MyCookie.HttpOnly = true;
MyCookie.Domain = ".domain.com";

// HttpCookie
HttpCookie MyCookie = new HttpCookie("sid");
MyCookie.Value = SID;
MyCookie.HttpOnly = true;
MyCookie.Domain = ".domain.com";

Response.Cookies.Add(MyCookie);
Run Code Online (Sandbox Code Playgroud)

c# asp.net cookies httpcookie cookiejar

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

在haskell中创建monad

我想创建自己的monad.这就是我写的:

data LeafConType a = LeafCon (a,Int,Int)

instance Monad (LeafConType ) where
return = LeafCon 
lc@(LeafCon (t,i,n)) >>= f = if i>=n
                                then lc
                                else f (t,i,n)
Run Code Online (Sandbox Code Playgroud)

但这不行.Ghc说:

leafcon.hs:26:1:
    Occurs check: cannot construct the infinite type: a = (a, Int, Int)
    When generalising the type(s) for `return'
    In the instance declaration for `Monad LeafConType'

leafcon.hs:27:1:
    Occurs check: cannot construct the infinite type: a = (a, Int, Int)
    When generalising the type(s) for `>>='
    In the instance declaration for `Monad LeafConType' …
Run Code Online (Sandbox Code Playgroud)

monads haskell

10
推荐指数
2
解决办法
5447
查看次数