Like Tree1Likes
  • 1 Post By Adak

Beginner C problems (Just started learning)

This is a discussion on Beginner C problems (Just started learning) within the C Programming forums, part of the General Programming Boards category; Hey, this is my first post on this forum so tell me if anything I write is against the forum ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    9

    Beginner C problems (Just started learning)

    Hey, this is my first post on this forum so tell me if anything I write is against the forum rules..

    Anyways, I'm currently doing a course for beginner C and Python (C section at the moment - I know some beginner Java, C#, blah, blah aswell) but one problem they keep using code like this

    Code:
    int m[4][4] = {{1,3,5,7}, {11,33,55,77}, {2,4,6,8}, {22,44,66,88}};
    int (*parr)[4] = m;
    (The above fine I've worked with multidimensional arrays before however)

    Here's where I get confused does this mean that +2 applies to the first bracket or the second or both, would be nice to keep it simple.. (m[1][1] = 33 - I hope)
    Code:
    *(*m+2) = 55
    *(*(m+1)+1) =11
    *(m[1]+2) = 55
    (*(m+2))[3] = 6
    (*(parr+3))[2] = 4
    I've included some of my answers but really they're shots in the dark, I've done the majority of the programming and most of the theory type questions but I've just never seen this type of calling before...

    ps. If this goes against helping newbs with homework rule then just tell me and I'll go away

    Thanks again

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    welcome to the forum, Millsie.

    The great thing about programming, is you have everything you need to answer questions that arise.

    You know that *m is pointing to m[0][0] which is one, and that when you increment or decrement a pointer, it will increment or decrement according to the size of the data type it has.

    So make a little print statement, and see what your answers will be. They are your questions, after all. Own them. Nurture that senses of inquisitiveness, for all your programming work. Be that programming sleuth, that C detective.

    You'll learn much more that way, and you'll remember it better as well. Probably enjoy it much more, also.

    Glad you joined us, though.
    CommonTater likes this.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    9
    Oh wow, thanks for that when I posted this I had almost given up.
    Sooo much clearer!

    One more quick question when a function doesn't have any argument names
    Code:
    int compare_dates(struct date *, struct date *);
    How are you meant to program with it?
    Every example I find that has this format it doesn't show the code behind it..
    (I've tried assigning the pointers to variables but then that didn't work either)

    Thanks again.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    I get 5, 33, 55, 8, 66. But I had to compiler and run the thing. I must admit some of them tricky.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    While it's more common, in my experience, to include the names in the prototypes, one only needs the names in the actual function definition (where the meat of the function is) - so that you can actually use the parameters.

    The prototype can have no names, or even different names from the actual function.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    9
    Quote Originally Posted by nonoob View Post
    I get 5, 33, 55, 8, 66. But I had to compiler and run the thing. I must admit some of them tricky.
    Sweet, Got the same! Good to know I'm on the right track.

    Quote Originally Posted by mike65535 View Post
    While it's more common, in my experience, to include the names in the prototypes, one only needs the names in the actual function definition (where the meat of the function is) - so that you can actually use the parameters.

    The prototype can have no names, or even different names from the actual function.
    Interesting, I've just done some reading on prototype functions in C.
    So it's sort of like a grid reference to a later used function.

    Since the prototype function is
    Code:
    int compare_dates(struct date *, struct date *);
    Does that mean the actual function has to be in the format
    Code:
    int compare_dates(struct date *d1, struct date *d2)
    I wrote the program before this that didn't need/want prototype functions and that was ok, however this makes it so much more complicated.


    Thanks
    Last edited by Millsie; 05-19-2011 at 02:51 PM.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Millsie View Post
    Since the prototype function is
    Code:
    int compare_dates(struct date *, struct date *);
    Does that mean the actual function has to be in the format
    Code:
    int compare_dates(struct date *d1, struct date *d2)
    Thanks
    Yes. A prototype is telling you what that function does, and so the actual function definition needs to match that. The names of the arguments are irrelevant, but the number, order, and type need to match.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    9
    Jesus, I'm happy I registered on this forum.

    Just completed the program, needed to do some background research on how to properly use a struct when a pointers involved.

    Seriously thanks so much for your time.

    Actually, one more question. Hehehe

    I'm having a bit of a problem with pointers and repointing them to where I want.
    Code:
    int p = 11, q = 22;
    int *ptrp = &p, *ptrq = &q;
    int **ppp = &ptrp, **ppq = &ptrq;
    So,
    Code:
                 L2      L1      L0 
    *ppp = ptrp. **ppp = ptrp* = p
    *ppq = ptrq. **ppq = ptrq* = q
    right?


    So wouldn't I therefore be able to do this to find out what I need

    Code:
    void swap_ptr(int **ppp, int **ppq){
                                                 
        int *tempL1 = *ppp;      /* ptrp */
        int **temqL2 = **ppq;  /* q    */
    
       *tempL1 = &temqL2; /* Make ptrp point to q*/

    My goal is to reassign *ptrp to q, however the closest I've ever gotten to this is
    switching p and q around. (I've honestly been working on this problem for a full day and I'm still nowhere)

    ---Don't worry about responding to this post I just gave up on the programming question.. Unfortunate, looked quite easy... Hopefully it doesn't come back to bite me in the ass.
    Last edited by Millsie; 05-19-2011 at 07:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Just started learning...
    By Ignoramus in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2009, 10:08 PM
  2. My learning proccess started
    By vishnukumar in forum C Programming
    Replies: 6
    Last Post: 10-03-2009, 03:10 AM
  3. Hello, I just started learning C
    By Pupp3t Master in forum C Programming
    Replies: 2
    Last Post: 02-12-2009, 07:00 AM
  4. Just started learning C++
    By Matt51723 in forum C++ Programming
    Replies: 9
    Last Post: 12-20-2007, 12:20 AM
  5. Zero background in programming.Started learning C.
    By abhim25 in forum C Programming
    Replies: 6
    Last Post: 04-23-2006, 04:59 PM

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