pointer problem

This is a discussion on pointer problem within the C Programming forums, part of the General Programming Boards category; Hi, I wrote a small code to change the values of the entries of data. But it is not changing. ...

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

    pointer problem

    Hi,

    I wrote a small code to change the values of the entries of data. But it is not changing. Will you please point out my mistake over here ?

    The values of data[] are not changing even after function call.


    I think there is something to do with passing pointers....

    pls help me out..

    Thank you
    ~~~~~~~~~~~~~~~~~~~
    Code:
    #include <stdio.h>
    
    
    #define size 4
    
    void update(int *);
    main(){
    
    int data[]={1,2,3,4};
    
    update(&data[0]);
    
    
    getchar();
    }
    
    void update(int *data)
    
    {
    
    int changed_data[size];
    for (int j=0;j<size;j++)
    
    changed_data[j]=j+100;
    data=changed_data;
    //printf("");
    
    
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    You cannot change the base address of an array, if that's what you're trying to do. Also, the array "changed_data" will no longer be accessible after the end of the function call. Try updating values in the array directly:

    Code:
    void update(int *data)
    {
       int j;
       for (j=0;j<size;j++) /*AFAIK, "for (int j=0;" is not C*/      
          data[j]=j+100;
       
    }

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    data=changed_data;
    This is only updating the local copy of data (c passes parameters by reference), the data in main is not affected.
    you could either write directly to the passed array ( pointer ).
    Code:
    void update(int *data) {
        for (int j=0;j<size;j++)
            data[j]=j+100;
    }
    or malloc the space for the updated data in your function and return the pointer to the caller. But that would require that data in main would be a pointer not an array.
    Kurt

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    data=changed_data;
    This will not copy the content of the array into the data array
    it just modifoes the content of the data pointer, so you effectevly loose the address of the memory where the data should be stored.

    in your function remove usage of the local changed_data array
    Use data instead.

    PS. And together with data it is better to pass the size of the array, so your function knew it instead of guess it

    [edit]
    I was tooooo slow...
    [/edit]
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    Quote Originally Posted by ZuK
    (c passes parameters by reference),
    I think you mean "by value".

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by noodles
    I think you mean "by value".
    Sorry, I did.
    Kurt

  7. #7
    Registered User
    Join Date
    Dec 2006
    Posts
    12
    for (j=0;j<size;j++)

    data[j]=j+100;

    i did this way, as you guys suggested...thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  2. Another pointer problem
    By mikahell in forum C++ Programming
    Replies: 21
    Last Post: 07-20-2006, 07:37 PM
  3. Pointer problem
    By mikahell in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2006, 10:21 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. pointer problem
    By DMaxJ in forum C Programming
    Replies: 4
    Last Post: 06-11-2003, 12:14 PM

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