Thread: I'm new to this

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    5

    I'm new to this

    I cant seem to get the output from my string in the different block. Is it normal?
    If it is, is there any other way to get the output in the different block with it?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It's hard to say without seeing your code.

    What do you mean by 'block' ?
    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
    Nov 2020
    Posts
    5
    I’m told that cursive brackets meant for block.
    These are the codes kind of similar to my assignment.
    Code:
    #include <stdio.h>
    
    int main()
    {
    	{
      char filling[] = "Kaya";
    }
    	printf("I have chosen %s\n",filling);
      
      return 0;
    
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well yes, that isn't going to work.

    Scope (computer science) - Wikipedia

    Code:
    #include <stdio.h>
    
    char msg[] = "Unused";
    int main()
    {
        char msg[] = "hello";
        {
            char msg[] = "world";
            printf("Msg=%s\n",msg);  // The one at line 8
        }
        printf("Msg=%s\n",msg);  // The one at line 6
        return 0;
    }
    For each variable reference, the compiler works it's way back up the chain of nested scope blocks.
    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.

  5. #5
    Registered User
    Join Date
    Nov 2020
    Posts
    5
    Is there any other way that I can assign name to my variables across blocks?
    Because my assignment acquires me to make an order details after making the choice

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Syazzz
    Is there any other way that I can assign name to my variables across blocks?
    Why do you want to do that in the first place? Why not declare your variables near where you intend to use them?

    If you're not sure what I mean, show your code and we could tell you how to better structure it. For example, quite obviously your example code in post #3 could have been written without the unnecessary inner code block:
    Code:
    #include <stdio.h>
    
    int main()
    {
        char filling[] = "Kaya";
        printf("I have chosen %s\n",filling);
    
        return 0;
    }
    Quote Originally Posted by Syazzz
    Because my assignment acquires me to make an order details after making the choice
    It may well be that you should declare the necessary variables before both pieces of code. Or perhaps these pieces of code are in different functions, in which case you need to declare appropriate parameters and pass corresponding arguments.
    Last edited by laserlight; 11-01-2020 at 06:45 PM.
    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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Is there any other way that I can assign name to my variables across blocks?
    They're called functions.
    Code:
    void foo ( char *msg )
    {
        strcpy(msg,"hello");
    }
    
    int main()
    {
        char msg[10] = "";
        foo(msg);
        printf("Msg=%s\n",msg);
        return 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

Tags for this Thread