Thread: Difference between two codes(very basic)

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    4

    Difference between two codes(very basic)

    Code 1
    Code:
    #include<stdio.h>
    typedef struct 
    {
      int n;
      char *string;
    }sct;
    
    void fun(sct *);
    
    int main(void)
    {
      sct one;
      fun(&one);
      printf("%s\n",one.string);
      return 0;
    }
    
    void fun(sct *p)
    {
      char two[]="funny";
      p->string=two;
      printf("%s\n",p->string);
    }
    Output 1
    funny
    ��

    Code 2
    Code:
    #include<stdio.h>
    typedef struct 
    {
      int n;
      char *string;
    }sct;
    
    void fun(sct *);
    
    int main(void)
    {
      sct one;
      fun(&one);
      printf("%s\n",one.string);
      return 0;
    }
    
    void fun(sct *p)
    {
      //char two[]="funny";
      p->string="funny";
      printf("%s\n",p->string);
    }
    Output 2
    funny
    funny
    So where my basics are wrong??
    I need output 2 but it should in code 1's way.
    What changes can i make in code 1?
    thanks

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    In the first example you assign string to a local variable. This variable doesn't exist anymore after return from fun().
    in the second example you assign a char literal that is propably stored somewhere in read only memory.
    Kurt

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    4
    ok the variable two doesn't exist but the address of the string "funny" is already copied in
    p->string.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by subhasnagre View Post
    ok the variable two doesn't exist but the address of the string "funny" is already copied in
    p->string.
    But the data that this pointer points to is propably overwritten with the parameters for the call to printf
    Kurt
    Last edited by ZuK; 08-24-2008 at 01:23 PM. Reason: typo

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Quote Originally Posted by subhasnagre
    ok the variable two doesn't exist but the address of the string "funny" is already copied in
    p->string.
    Um, no. You are not copying the address of the string "funny", you are initializing the array with the contents of the string "funny". As Zuk pointed out, the array ceases to exist at the end of the function.

    THIS would be copying the address of the string "funny":
    Code:
    void fun(sct *p)
    {
      const char *two = "funny";
      p->string=two;
      printf("%s\n",p->string);
    }

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    4
    Ok I got it. Thanks for the replies.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Note that when assigning string literals the way you do, to a pointer, it's best to make that pointer a pointer to const char, since usually string literals cannot be modified. Making it const will help the compiler catch some of your mistakes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. BASIC vs. Qbasic
    By Jperensky in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 12-10-2002, 09:13 AM
  3. Difference between macro and pass by reference?
    By converge in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2002, 05:20 AM
  4. how to get difference of digits
    By Leeman_s in forum C++ Programming
    Replies: 5
    Last Post: 12-20-2001, 08:32 PM
  5. What is the Difference between ANSI C and non-ANSI C ?
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-24-2001, 06:55 AM