Thread: old failing brain

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    63

    old failing brain

    My c was not very good to begin with, but my old brain is failing me more and more lately.


    Are these two the same?

    Code:
    char* ss ;
    ss = GetInfo()
    if(ss && ss[3]) == '2'
    
    
    if (ss && ss[3]) == 50
    James

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    They are, if you assume an ASCII character set.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    63
    Thank you!

    James

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    In addition to what @Salem said, "ss" should be pointing to a legitimate character array.

    As written, ss is uninitialized.

    I'm not sure what "GetInfo()" is.

    Please provide a small but complete program that can be compiled and tested.

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    63
    More code is irrelevant.
    Salem gave the answer to the question asked.

    James

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Hmm... but your code is syntactically incorrect to begin with. I don't see why you couldn't have written a program like this:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        printf("%d=50?\n", '2');
        return 0;
    }
    Then upon compiling it, running it, and seeing that the output was:
    Code:
    50=50?
    ask if it was guaranteed that '2' == 50, upon which Salem's answer would apply.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    63
    Thanks guys I do appreciate the info but....
    I am working on a new Basic -> c/c++ translator base on the original BCX.
    There is no provision for parsing single quotes that are so prevalent in c code, as in the example.


    I can use ASC("2") instead of '2' which mimics the c nomanclature and translates to 50.


    So my Basic code is:
    Code:
    If ss AND ss[3] = ASC("2") Then
    	'do your stuff
    End If

    And it translates to:


    Code:
    if(ss && ss[3]) == 50
    {
    	// do your stuff
    }

    James

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > If ss AND ss[3] = ASC("2") Then

    You could translate it to
    if(ss && ss[3]) == "2"[0]
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Left Brain v Right Brain
    By stevesmithx in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-30-2008, 09:13 AM
  2. brain vs. computer (brain wins hands down)
    By hk_mp5kpdw in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 09-17-2003, 08:41 PM
  3. Brain Malfunction
    By Bazza in forum C Programming
    Replies: 13
    Last Post: 07-12-2002, 02:45 PM
  4. Brain fart
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 08-30-2001, 12:40 PM