ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Man-- This is not a homework. I swear not. I was just trying to meddle with all pointer operations and I ended up with the above code. I swear this has has nothing to do with my studies part.
OK atleast confirm my recent find.
In --- b=(int *) a; and c=(int **) a;
the latter type cast operator makes no impact on pointer arithmetic
since no info about size of cols is specified. So on incrementing it does behave in the same way as b.
i think in the assignment for 'b' and 'c' you are casting it to the starting position of the array 'a'.
logically, 'c' is a pointer to an address that points to the memory address of where 'a' lives so i think it is accurate that 'b' and 'c' reference the same memory.
Obviously printing out the addresses would solve your dilemma...
Both are assigned to a, which is the address of the starting location of the array.
b will treat this as an address and so looking at b will give the first element of the array.
c is an pointer to a pointer and so it will treat the first element of the array as a pointer.
Printing out b will give the value held at a[0]
Printing out c will give the value pointed by a[0], which may not be what you want to do but that is what you have told the compiler you want to do.
Or at least that's how I read it... been wrong before
Your code is of very poor quality, comments follow:
Code:
#include<stdio.h>
#include<malloc.h> /* Non-standard header that isn't used, remove it. */
void main() /* main() has a return type of int */
{
/* Here you declare a two-dimensional array that holds a total of nine
elements, but you only initialize seven of them. The last two gets
whatever the compiler puts there, often 0 if you compile in debug mode.
Also, one usually puts a pair of {} around each row. If you had bothered
to compile with warnings turned on you would've noticed the compiler
complaining about this, among other things. */
int a[][3]={ 1,2,3,4,5,6,7 };
int *b;
int **c;
b=(int *) a;
c=(int **)a;
/* Using a nested loop here is ridicolous since
you only use the postincrement-operator in
the call to printf(). Use a single loop instead
and move the loop-variable declaration to the top
of the main() function to be compliant with older
C standards. */
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
printf(" %d %d",*b++,*c++);
/* Since main() has a return type of int, you should
return 0 here. */
}
A modified version of your original code:
Code:
#include <stdio.h>
int
main(void)
{
int a[][3]={ {1,2,3},{4,5,6,},{7,8,9} };
int *b = (int *)a;
int **c = (int **)a;
int i = 0;
int num_elements = sizeof(a) / sizeof(int);
for(i = 0; i< num_elements; ++i)
printf("*b = %d *c = %d\n", *b++, *c++);
return 0;
}
Now, let's explain what happens.
Code:
*b; /* Yields an int, because b is of type pointer-to-int. */
**c; /* Yields a-pointer-to-int, because b is of type pointer-to-pointer-to-int. */
However, the value of each is taken from the 2d-array a. So, at the first iteration of the loop
*b yields 1 (of type int, the first element in a). *c yields 0x01 of type int*, but you ignore the compilers warning of printing a pointer type as an int, so it prints 1 (and 2, and 3 etc).
Calling printf() and similar varargs-functions with invalid format specifiers causes undefined behaviour.
1. That was just an attempt to study the behaviour of pointers.
So I didnt concentrate much on statndards.
2.I never knew malloc.h is extinct. My compiler permitted me to run the program only after the inclusion of this header file.
3. Back from C++ and java the habit of local declarations has caught me(I think this approach is clear)
4. loop--Previously I was trying to fetch values using both the variables i and j using pointer to an array of dimension 3
5. Regarding array declaration that too was an attempt to see how the compiler behaves As you said 0's were initialized for 3rd dimn and 4 higher dimns garbage
6. And I didnt get any warning from my compiler for the *c retrieval
7. And how the value 0x01 sprang up(from air!). Either the comipler has to say that referring to the contents of non-pointer variable is an error(on any other instance this would happen) or it should give garbage values. Iwonder how u got this value
8. I feel extremely sorry to post such an ill-prepared program
But I hope u understand from my prev 2 posts that I am trying to get myself clear about some of the conventions of C.
Anyway, thanks for your response.
And please give explanation for the (7)
--------------
Presently I use MS VisualStudio due to non-availability of a good IDE for linux.(in the sense that I dont have one now)
2.I never knew malloc.h is extinct. My compiler permitted me to run the program only after the inclusion of this header file.
I dont think malloc.h is extinct I think what he was saying is that it is not a base header ie almost all c programs will require stdio.h but malloc.h is only required where you are going to be allocating the memory and in this case you are not using any of the malloc functionality so there is no reason to include it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.