1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

pointers C language help ask

Discussion in 'C' started by gbrunati, Apr 22, 2011.

  1. gbrunati

    gbrunati New Member

    Joined:
    Apr 22, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I want to print a text using it's var name call, but what I get is the name itself. Help me, please...I think it's about pointers.

    I need to select and print one from a group of texts, and "chnitxpr" is constructed in a separate subroutine, so it is the name of the choosen text variable ("vchnitxpr" in this case). My problem is that argument 2 in the "strcpy" is recognized as the name of the variable string and I want the variable itself, so it prints the corresponding text.

    Trying to explain widely, this is the code with more details:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    char chnitxpr[9];
    char* fPlnNcs (pto_cel pcpto);
    
    int main()
    {
    char chvar[9];
    char *chNepC01 = "01blablablsblablablablsblablablablsblablblblaaabla0101010101.\n";
    char *chNepC02 = "02blablablsblablablablsblablablablsblablblblaaabla0202020202.\n";
    char *chNepC03 = "03blablablsblablablablsblablablablsblablblblaaabla0303030303.\n";
    ...
    /* fPlnNcs(sol) = chnitxpr could be chNepC01, chNepC02, chNepC03...  chNepC0N */
    
    	fPlnNcs(sol); /* this is a string */
    	strcpy(chvar, fPlnNcs(sol));
    	printf("%s\n", chvar);
    
    	or...
    
    	fPlnNcs(sol); /* this is a string */
    	strcpy(chvar, chnitxpr);
    	printf("%s\n", chvar);  /*:mad: I want to print the text, but prints the name of the variable */
    
    	return 0;
    }
    
    char* fPlnNcs (pto_cel pcpto) /* Ubica punto celeste en casa (21 de abril 2011)*/
    {
    ...
    ...
    	return chnitxpr; 
    }
    
    thanks in advance for your help.
     
  2. eriyer

    eriyer New Member

    Joined:
    Jan 22, 2011
    Messages:
    32
    Likes Received:
    0
    Trophy Points:
    0
    chvar is of size 9 and is declared as a local variable i.e. created on the stack - the strings (chNep*) are much longer. strcpy() could be overwriting the stack and what you see as output could be junk from the RAM.
     
  3. gbrunati

    gbrunati New Member

    Joined:
    Apr 22, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Let's forget the chvar local variable and the strcpy.
    ;)

    The return from the function is a size 9 string wich is the MANE of the longer string variable to print. I want to print the text, i.e., the choosen string (*chNep...), not the name of the shorter variable.
     

Share This Page