Using pointers to communicate between functions - re char *

This is a discussion on Using pointers to communicate between functions - re char * within the C Programming forums, part of the General Programming Boards category; We all know that we can change the values of two integers x and y using the follwing code: Code: ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    98

    Using pointers to communicate between functions - re char *

    We all know that we can change the values of two integers x and y using the follwing code:
    Code:
    void interchange(int *u, int *v) {
      int temp;
      temp = *u;
      *u = *v; 
      *v = temp;
    }
    int main() {
      int x=5, y= 10;
      interchange( &x, &y);
    }
    That is, you pass the address of x (&x) to a function that has a parameter *x.
    What if x is itself a pointer?
    Code:
    void func ( char **array1, char *array ) {
      array1 = array;
    }
    int main ( ) {
      char *msg;
      char *msg1;
      msg = malloc(20);
      sprintf(msg, "hello world");
      func( &msg1, msg );
    }
    What I really want to achieve is msg1 = msg. But gcc isssues two warnings:
    1. " assignment from incompatible pointer type" for 'array1 = array'
    2. "passing arg 1 of `func' from incompatible pointer type" for 'func( &msg1, msg )'
    And if I print msg1 at the end, I get a segmentation fault.

    [Edit]Ooops. I got it. I should have said *array1 = array;
    Now there is no seg fault. But warning #2 remains. Why?
    Last edited by hzmonte; 04-18-2006 at 01:06 AM.

  2. #2
    Registered User vinit's Avatar
    Join Date
    Apr 2006
    Location
    India
    Posts
    39
    No warning appeared to me.Im using gcc 3.4.3 on RHEL4,even I turned -Wall option.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    #include<stdio.h>
    
    void func ( char **array1, char *array ) {
      *array1 = array;
    }
    int main ( ) {
      char *msg;
      char *msg1;
      
      msg = malloc(20);
      msg1 = malloc(20);
      
      sprintf(msg, "hello world");
      func( &msg1, msg );
      printf("%s",msg1);
      
      return 0;
    }
    /*myoutput
    hello world
    */
    ssharish2005

  4. #4
    Registered User vinit's Avatar
    Join Date
    Apr 2006
    Location
    India
    Posts
    39
    I have'nt allocated memory for msg1,then also it gives same output.As Im not accessing further elements from msg1 it works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  4. question about functions and char *
    By Bittrexx in forum C Programming
    Replies: 4
    Last Post: 07-22-2003, 12:27 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21