# 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. ## 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. 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.

3. 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. I get 5, 33, 55, 8, 66. But I had to compiler and run the thing. I must admit some of them tricky.

5. 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. Originally Posted by nonoob
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.

Originally Posted by mike65535
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

7. Originally Posted by Millsie
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.

8. 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.