Thread: pointers doubt

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    10

    pointers doubt

    hello friends

    i'm trying to generate in an intermediate code (almost C) a function, with labels and gotos and i have this problem with the pointers (maybe i wont never understand pointers).

    Code:
    #include <stdio.h>
    
    int main() {
    
    char *puntRetorno="";
    char *point = &puntRetorno;
    
    printf("empezando\n");
    puntRetorno="i";
    goto ESCRIBIR;
    
    
    i:
    printf("fin\n");
    return 0;
    
    ESCRIBIR:
    printf("hola\n");
    //valorRestorno=0;
    goto retorno;
    
    retorno:
    printf("Ya casi: %s\n",point);
    goto *point;
    how can i do to make the pointer *point points to the char puntRetorno has inside?

    I've tried so many ways but nothing...

    Thank you very much

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Okay, imagine the flow of a C program: it generally starts at main() and moves from top to bottom. goto changes the flow of the program by moving execution to a labeled line. A label can only be a descriptive tag, so you can't goto a variable, like you were doing with point.

    goto does have some uses (particularly if you like single return points) but other than that, the lack of a solid code structure makes things hard to read. Look up what "spaghetti code" means.

    You need to rethink whatever you are trying to do.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    how can i do to make the pointer *point points to the char puntRetorno has inside?
    Assuming you mean, "how can I make point point to puntRetorno" -- you already did that.
    Code:
    char *point = &puntRetorno;
    Of course, you didn't do it correctly, but hey. puntRetorno is already a pointer; when you assign something to point it's expecting a pointer; so just use puntRetorno by itself.
    Code:
    char *puntRetorno="";
    char *point = puntRetorno;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    10
    Sorry but I think I'm not explaining myself very well...

    I need to do that because I'm making a compiler for PHP in a intermediate code that I have to compile then with gcc.

    To translate functions to this intermediate code I need to use labels and gotos.

    dwks, I have to do: "goto *point" to use this label anytime, can i do that?
    I just thought *point could has inside a different string each time i need.

    thank you

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > char *puntRetorno="";
    > char *point = &puntRetorno;
    That should be char **point

    > goto *point;
    This will never work in C, it is a compiled language not an interpreter.

    The best you can manage in C is something like this, to turn a keyword into an identifier
    Code:
    #include <stdio.h>
    #include <string.h>
    
    /* use a table for large numbers of cases */
    int lookup ( const char *s ) {
        if ( strcmp(s,"hello") == 0 ) return 0;
        if ( strcmp(s,"world") == 0 ) return 1;
        return 2;
    }
    
    int main()
    {
        char    *word = "hello";
        switch ( lookup(word) ) {
            case 0:
                printf("hello\n");
                break;
            case 1:
                printf("world\n");
                break;
            default:
                printf("Huh?\n");
                break;
        }
        
        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.

  6. #6
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Con mi compiler, no puedo usar una variable con "goto". Intenta:
    Code:
    #define point i
    Pero, este no compilara:
    Code:
    printf("Ya casi: &#37;s\n",point);
    No pienso que tu puedas hacer que tu estas intentando hacer. Quizas en un idioma diferente.
    Don't quote me on that... ...seriously

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Salem View Post
    > goto *point;
    This will never work in C, it is a compiled language not an interpreter.
    That has nothing to do with it. There's no reason such a feature couldn't easily exist in C, it just doesn't. In fact, the GCC compiler does allow you to do this (with almost, if not exactly, the same syntax as used here), as an extension to the C language. You "take the address" of a label, and then goto that address. Quite useful for implementing large jump tables, which are rampant in lots of system-level software like the Linux kernel.

    But obviously, as it is non-standard, it isn't something to rely on for typical programming tasks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM