Thread: Change value in function

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    116

    Question Change value in function

    I have two code, one is true, and other is wrong. (the purpose of this work is test, how to change an array in function.
    First is:
    Code:
    #include <stdio.h>
    int array(int a,int b,int *x);
    int main(){
        int *a;
        array(5,6,a);
        printf("%d %d\n",a[1],a[2]);
    }
    int array(int a, int b,int *x){
        x[1]=a;
        x[2]=b;
    }
    and second is:
    Code:
    #include <stdio.h>
    int array(int a,int b,int *x);
    int main(){
        int a;
        array(5,6,&a);
        printf("%d %d\n",a[1],a[2]);
    }
    int array(int a, int b,int *x){
        x[1]=a;
        x[2]=b;
    }
    First code is true (it will print 5 6), but second code is wrong. It's wrong at line
    Code:
     Printf("%d %d",a[1],a[2]);
    // It has an error name:invalid type int[int] for array subscript
    I have google but I don't know so much about this problem.
    So, who can help me, why It's wrong, please.

    thanks so much
    Last edited by hqt; 08-14-2011 at 11:37 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    a is not an array of integers, and lying to your other function about it won't make it so.

    Your first function is even worse.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I have two code, one is true, and other is wrong.
    Well the first only works because of pure dumb luck, since your pointer is uninitialised.

    If you had
    int *a = NULL;

    it would probably blow up.

    Further, you seem to be implying that arrays start at [1], when in fact they start at 0.

    The minimal you need to do to make this work is either
    int a[3];

    or
    int *a = malloc( 3 * sizeof(*a) );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    116

    Question

    @Salem: thanks a lot, I know where my mistake, your advice is very helpful (It can run smooth now). But I have one point that I don't know: why in my old first code, why I have "pure dumb LUCK" and why when I had: int *a=NULL, my program will be blow up.
    thanks again
    Last edited by hqt; 08-15-2011 at 12:21 AM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    On most machines, dereferencing NULL is a fatal error.

    The pure dumb luck is like this
    Code:
    int age;
    printf("You are %d years old\n", age );
    It's easy to see that this is wrong.

    But with your uninitialised pointer, all that you've really got to go on is whether it crashes or not. Any random address is good enough (for you), so long as it doesn't crash.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    116
    @Salem: Oh,now I know why a=NULL will blow up. But, if I don't have line
    Code:
    int *a=malloc(sizeof(*a));
    the most problem is we don't know exactly the address of a(a[1],a[2],etc). So (if this thinking of mine is true), I don't think this is a dumb because we doesn't need it in most case. Am I wrong? please tell me more, please.
    Sorry, if this is a very silly question.
    thanks for tons

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need it in every case (the initialization of a, that is). 0.001% of the time, you are going to make a point at something already existing, so malloc isn't necessary (but you still need to do initialization). The other 99.999% of the time, you will need to use malloc to obtain a valid pointer.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You don't actually need to know --and should not care-- what the actual address assigned to a variable is.

    What is important is that you do not play with uninitialized pointers. You need to use malloc() or or assign it to the address of another variable before using it. Playing with unintialized anything is probably the most common cause of run time errors.

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    116
    Oh, I luckily read a paper that say about Pointer. After that, I see some above questions of mine is really silly A "bad pointer" is reference to random address. It hard to see problem when debug small problem, but it will be really an awful event if run this module in big project. (Am I thinking right :"> )
    Last edited by hqt; 08-15-2011 at 11:02 AM.

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    I think you need to go through whatever C book or reference you have, page by page, problem by problem. You have some serious level of knowledge problems.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-28-2011, 07:57 AM
  2. Change function
    By cody.carter in forum C Programming
    Replies: 19
    Last Post: 05-17-2010, 11:19 AM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  4. How do i change this into a function?
    By MrX8503 in forum C++ Programming
    Replies: 3
    Last Post: 04-19-2004, 12:49 AM
  5. how to change it to function
    By lala in forum C Programming
    Replies: 2
    Last Post: 10-06-2002, 11:15 AM