Thread: string arrays

  1. #16
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >What does it serve to place a "*" before a function name.

    It declares the function to be returning a pointer of some type. The function strcat returns a pointer to char.

    >Furthermore, what does "const" before char *s2 mean? Am i right to say it means what ever string s2 is pointed to cannot be change. It must remain constant.

    Yes. The prototype makes a promise to you, the caller, that the function will not modify what is pointed to by s2.

    >what is the difference between static and global variables?

    Does this help?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  2. #17
    Registered User
    Join Date
    Sep 2003
    Posts
    133
    ok,
    what i learn so far is that the compiler allocates memory to global variables as soon as it reaches that declaration.

    Declaring static variables globally mean it can only be used in that file only. What difference will it be if it is declared in a function definition? Am i able use the value of that static variable in other functions other rather the one it is declared in?

  3. #18
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >What difference will it be if it is declared in a function definition? Am i able use the value of that static variable in other functions other rather the one it is declared in?

    No.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #19
    Registered User
    Join Date
    Sep 2003
    Posts
    133
    sorry regarding the code again:
    Code:
    char *strcat(char *s1, const char *s2);
    function strcat will return a pointer of type char

    so does it mean
    Code:
    int *ptr;
    ptr = strcat(s1,s2);
    will assign an address to ptr to point to.

  5. #20
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    int *ptr;
    ptr = strcat(s1,s2);
    You want ptr to be of type char* since that is what strcat returns.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
       char s1[20] = "hello";
       char s2[]   = " world";
       char *ptr;
       ptr = strcat(s1,s2);
       puts(ptr);
       return(0);
    }
    
    /* my output
    hello world
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #21
    Registered User
    Join Date
    Sep 2003
    Posts
    133
    oh ok thanks...my bad... din mean it to be int...careless.
    but why do u use

    Code:
    return (0);
    what (0) means? is it considered as an integer?
    can i just use
    Code:
    return 0;
    ?

  7. #22
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >can i just use
    > return 0;
    >?

    Yes.

    Hmmm. I must've copied that part of the code from somewhere else. I used to always parenthesize return values, but I moved away from that style a while ago.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #23
    Registered User
    Join Date
    Sep 2003
    Posts
    133
    thanks very much for the patience and guidance. I've been asking very basic qns cuz i am relying on a textbook with inadequate information.

  9. #24
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Dave_Sinkula
    >What difference will it be if it is declared in a function definition? Am i able use the value of that static variable in other functions other rather the one it is declared in?

    No.
    What? What are you talking about? What would be the point of returning static variables if you couldn't use them in other functions? Of course you can use them in other functions.

    Code:
    char *foo( void )
    {
        static char buf[1024];
    
        if( buf[0] )
            printf( "%s\n", buf );
    
        memset( buf, 0, 1024 );
    
        return buf;
    }
    
    int main( void )
    {
        char *ptr;
    
        ptr = foo( );
    
    	*ptr++ = 'S';
    	*ptr++ = 'e';
    	*ptr++ = 'e';
    	*ptr++ = '?';
    
        foo( );
        return 0;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  10. #25
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >What are you talking about?
    Declaring static variables globally mean it can only be used in that file only. What difference will it be if it is declared in a function definition? Am i able use the value of that static variable in other functions other rather the one it is declared in?
    Since it was referred to in the context of a comparison to symbols with file scope, I assumed Raison meant referencing a symbol outside of the block scope in which it was declared static.
    Code:
    int main( void )
    {
       strcpy(buf, "See?"); /* Error: Undefined symbol 'buf' in function main */
       return 0;
    }
    >>Am i able use the value

    But I guess that's not what was asked.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #26
    Registered User
    Join Date
    Sep 2003
    Posts
    133
    I guess Quzah cleared my doubts...sorry my qn was not clear enough.

  12. #27
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Dave_Sinkula
    Since it was referred to in the context of a comparison to symbols with file scope, I assumed Raison meant referencing a symbol outside of the block scope in which it was declared static.
    Ah gotcha. Totally see where you were coming from, and in that way you're 100% correct. 'static' doesn't make them like globals, unless you happen to be using static globals.

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #28
    Registered User
    Join Date
    Sep 2003
    Posts
    22

    Crazy Array Questions!

    Hey Hammer,
    I'm sorry if i have offended you or anyhting or anyone. I don't know much about c programming, ,but i believe getting help and directions from people who know would help me get there.
    If you can't help it's ok, but someone might be interested in helping. We've all been in situations sometime!
    Thanks.
    M

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM