在Stack Overflow播客中,Joel Spolsky经常对杰夫阿特伍德强调Jeff不知道如何用C编写代码.他的声明是"知道C可以帮助你编写更好的代码." 他还总是使用某种涉及字符串操作的故事,以及如何知道C将允许您用不同的语言编写更有效的字符串例程.
作为一个知道一点C但又喜欢用perl和其他高级语言编写代码的人,我从来没有遇到过我能够通过编写C来解决的问题.
我正在寻找现实世界情况的例子,在这些情况下,在用高级/动态语言(如perl或python)编写项目时,了解C会很有用.
编辑:阅读你们提交的一些答案很棒,但在这方面对我来说仍然没有任何意义:
以strcat为例.在C中组合字符串有正确的方法和错误的方法.但为什么我(作为高级开发人员)认为我比Larry Wall聪明?为什么语言设计者不会以正确的方式编写字符串操作代码?
我被要求在http://twitter.com/jonathanjulian上发布一个关于StackOverflow的问题,然后被其他几个人转发.我已经有一个丑陋的解决方案,但我按要求发布了原始问题.
所以这是背景故事.我们有一个庞大的数据库应用程序,它专门用于客户端视图的ExtJS.我们使用GridPanel(Ext.grid.GridPanel)来处理从远程存储加载的行视图.
在我们的每个接口中,我们还有一个FormPanel(Ext.form.FormPanel),它显示一个允许用户从GridPanel创建或编辑记录的表单.GridPanel列绑定到FormPanel表单元素,以便在GridPanel中选择记录时,所有值都填充在表单中.
在每个表单上,我们有一个表行ID(主键)的输入字段,其定义如下:
var editFormFields = [
{
     fieldLabel: 'ID',
     id: 'id_field',
     name: 'id',
     width: 100,
     readOnly: true, // the user cannot change the ID, ever.
     monitorValid: true
} /* other fields removed */
];
Run Code Online (Sandbox Code Playgroud)
所以,这一切都很好,很好.这适用于我们所有的应用程序.在构建新界面时,要求我们需要使用第三方文件存储API,该API以在IFrame中加载的小网页的形式提供界面.
我将IFrame代码放在FormPanel的html参数中:
var editForm = new Ext.form.FormPanel({
   html: '<div style="width:400px;"><iframe id="upload_iframe" src="no_upload.html" width="98%" height="300"></iframe></div>',
   /* bunch of other parameters stripped for brevity */
});
Run Code Online (Sandbox Code Playgroud)
因此,每当用户选择记录时,我都需要将IFrame的src属性更改为我们正在使用的服务的API URL.沿着线的东西http://uploadsite.com/upload?appname=whatever&id= {$ id_of_record_selected}
我最初进入id字段(粘贴在上面)并添加了一个更改侦听器.
var editFormFields = [
{
     fieldLabel: 'ID',
     id: 'id_field',
     name: 'id',
     width: …Run Code Online (Sandbox Code Playgroud) 我正在编写一个非常简单的Arduino类来控制两个电机.
我的头文件Motor.h中有一个简单的类定义:
class Motor 
{
  public:
    Motor();
    void left(int speed);
    void right(int speed);
    void setupRight(int rightSpeed_pin, int rightDirection_pin);
    void setupLeft(int leftSpeed_pin, int leftDirection_pin);
  private:
    int _rightMotorSpeedPin;
    int _rightMotorDirectionPin;
    int _leftMotorSpeedPin;
    int _leftMotorDirectionPin;
};
Run Code Online (Sandbox Code Playgroud)
在我的主库文件Motor.cpp中,我有以下类构造函数:
Motor::Motor() {
  // Intentionally do nothing.
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用以下行在我的主程序中初始化我的类时:
Motor motor();
Run Code Online (Sandbox Code Playgroud)
我收到以下编译错误:
MotorClassExample.ino: In function 'void setup()':
MotorClassExample:7: error: request for member 'setupRight' in 'motor', which is of non-class type 'Motor()'
MotorClassExample:8: error: request for member 'setupLeft' in 'motor', which is of non-class type 'Motor()'
request for …Run Code Online (Sandbox Code Playgroud) 我正在构建一个非常动态的基于Web的应用程序,它使用许多Javascript来处理用户事件。我正在使事情变得更有用,并且遇到了我从未遇到过的问题。
我正在使用jQuery,因此请考虑到您的答案。提前致谢。
我有一组按钮元素定义为:
<input type="button" title="My 'useful' text here." disabled="disabled" />
Run Code Online (Sandbox Code Playgroud)
这些按钮的默认样式为:
div#option_buttons input {
     cursor: help;
}
Run Code Online (Sandbox Code Playgroud)
然后,使用jQuery在选择框上将以下内容作为单击事件运行:
window.current_column = '';
$('select.report_option_columns').live('click', function() {
    var column = $(this).val();
    if ( column == window.current_column ) {
        // clear our our previous selections
        window.current_column = '';
        // make this option no longer selected
        $(this).val('');
        $('div#option_buttons input').attr('disabled','disabled');
        $('div#option_buttons input').attr(
            'title',
            'You must select a column from this list.'
        );
        $('div#option_buttons input').css('cursor', 'help');
    } else {
        window.current_column = column;
        $('div#option_buttons input').attr('disabled','');
        $('div#option_buttons input').attr( …Run Code Online (Sandbox Code Playgroud) 我是ActionScript开发的新手,我正在使用FlashDevelop IDE.我一直在玩一些非常简单的东西,并遇到了一个我似乎无法解决的问题.
我的应用程序编译并运行,并且监视单击事件的函数完全触发,当我将它传递给trace()时,我可以在控制台中看到该事件,但是看着KeyboardEvent的相同代码根本无法触发.
这是我的代码:
package GameTesting
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.events.KeyboardEvent;
    [Frame(factoryClass="GameTesting.Preloader")]
    public class Main extends Sprite
    {
        public function Main():void
        {
            if (stage) {
                init();
            } else {
                addEventListener(Event.ADDED_TO_STAGE, init);
            }
        }
        private function init(e:Event = null):void
        {
            removeEventListener(Event.ADDED_TO_STAGE,init);
            addEventListener(MouseEvent.CLICK, onClickEvent);
            addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownEvent);
        }
        private function onKeyDownEvent(e:KeyboardEvent):void
        {
            trace(e);
        }
        private function onClickEvent(e:MouseEvent):void
        {
            trace(e);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)
每次按预期触发MouseEvent trace(),但无论我按什么键,KeyboardEvent都不会触发.有任何想法吗?