Once again I am wrong

This is a discussion on Once again I am wrong within the C Programming forums, part of the General Programming Boards category; Working on another assignment and I am confused. I am sure my problem is transferring the array from main to ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    TX
    Posts
    19

    Question Once again I am wrong

    Working on another assignment and I am confused. I am sure my problem is transferring the array from main to the display function, but can't seem to make it work right for me. I have been working on this for several hours and know that if I continue I may end up with a broken computer. Can someone point out my mistake (which I am sure is obvious but I am not seeing it). Thank you for the help in learning how to program correctly.


    Code:
    #include <stdio.h>
    
    int *cptr, i;
    int channel[7];
    
    int display (*int);
    
    int main()
    {
    *cptr = &channel[0]
    channel[7]= {2,4,5,7,9,11,13};
    display (*cptr);
    
    return 0;
    }
    
    int display (int *cptr)
    {
    int *cptr, i;
    for (i=0; i<=6; ++i)
    printf ("\nElement %d is %d", i, *(cptr + i));
    
    system ("Pause");
    
    return 0;
    }
    And there is the problem of mine

  2. #2
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    Well I see a blatant one. Are you missing a semi-colon.

    /edit
    Why is *cptr, and i global and in display?

    That should get you started!

    /edit2
    Look a your errors when you try to compile they will help you out!

    /edit3
    I am sure my problem is transferring the array from main to the display function,
    You've declared *cptr, and i as global variables. Sounds to me like you don't really want that!
    Last edited by Bajanine; 12-18-2006 at 08:04 PM.
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    7
    *cptr = &channel[0] // cptr = channel;
    display (*cptr); // display (cptr);
    int *cptr, i; // int i;

    modify this

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    TX
    Posts
    19
    Okay that clears up most of my issues, but now I have a new one.

    Code:
    #include <stdio.h>
    
    int *cptr, i;
    int channel[7];
    
    int display (int);
    
    int main()
    {
    cptr = channel;
    channel[7]={2,4,5,7,9,11,13};
    display (cptr);
    
    return 0;
    }
    
    int display (int cptr)
    {
    int i;
    for (i=0; i<=6; ++i)
    printf ("\nElement %d is %d", i,(cptr + i));
    
    system ("Pause");
    
    return 0;
    }
    the compiler tells me there is an error before the { in line 11. I can not for the life of me figure out what it is. I have been looking at my references and it looks correct to me. Can anyone else see what my noob eyes are missing?
    Last edited by al_engler; 12-18-2006 at 09:17 PM.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    28
    hey buddy,
    you are passing integer pointer to display() function,
    make that to
    int display(int *cptr) and also make changes to prototype of this function to
    int display(int *);

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    channel[7]={2,4,5,7,9,11,13};
    The initialization should be there the variable is defined. You cannot initialize array in that way afterwards

    Code:
    int display (int *cptr)
    {
    int i;
    for (i=0; i<=6; ++i)
    printf ("\nElement %d is %d", i,*(cptr + i));
    
    system ("Pause");
    
    return 0;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    Code:
    channel[7]={2,4,5,7,8,11,13};
    Code:
    channel[7]="2,4,5,7,8,11,13"
    The change allouded the code to complile, the result was not that you wanted it to be. It would have to do with you at decaring that the channel[7] array at row 1, column 6 is 2,4,5,7,8,11,13. So you are not declaring you string for the array but your string for a single memlocation.
    You rant and rave about it, but at the end of the day, it doesn't matter if people use it as long as you don't see.
    People are free to read the arguments, but if the only way for you to discover gravity is by jumping off a cliff, then that is what you're going to have to experience for yourself.
    Eventually, this "fast and loose" approach of yours will bite you one too many times, then you'll figure out the correct way to do things. - Salem

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    TX
    Posts
    19
    Thank you for all the help. I have another question though ( I am really new to all this). What does the warning " warning: assignment makes integer from pointer without a cast " mean? I have this on a couple different lines of code and don't totally follow what it is telling me.

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    it means that you trying to assign something like:
    Code:
    int* p = NULL;
    int a = p;
    and in most cases it is wrong thing to do - there can be even not enough space in the int to store the value stored in the pointer... And Moreover, I don't think it is want you actually want to do... So you should pay a special attention to all this warnings to undestand what you are doing wrong
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

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