LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-06-2005, 10:30 AM   #1
vivekr
Member
 
Registered: Nov 2005
Location: Coimbatore,India
Distribution: Fedora Core4
Posts: 68

Rep: Reputation: 15
Pointer wonder


To all my dear buddies I am puzzled by the behaviour of the following code

Code:
#include<stdio.h>
#include<malloc.h>
void main()
{
	int a[][3]={ 1,2,3,4,5,6,7 };
	int *b;
	int **c;

	b=(int *) a;
	c=(int **)a;

	for(int i=0;i<3;i++)
		for(int j=0;j<3;j++) 
			printf(" %d %d",*b++,*c++);
}
Here both *b and **c behave in a similar fashion. Can any one help me out. How?. Thanks

----
a small mistake was committed

Last edited by vivekr; 12-06-2005 at 10:31 AM.
 
Old 12-06-2005, 11:32 AM   #2
Dextrose
LQ Newbie
 
Registered: Dec 2004
Location: Orange County, CA
Distribution: Fedora Core 3
Posts: 8

Rep: Reputation: 0
Looks like a homework question to me.
 
Old 12-06-2005, 11:37 AM   #3
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
deleted my post under suspicion of homework.

Last edited by schneidz; 12-06-2005 at 11:40 AM.
 
Old 12-06-2005, 04:39 PM   #4
vivekr
Member
 
Registered: Nov 2005
Location: Coimbatore,India
Distribution: Fedora Core4
Posts: 68

Original Poster
Rep: Reputation: 15
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.

Correct?
 
Old 12-06-2005, 04:54 PM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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.

anyone else...
 
Old 12-06-2005, 06:00 PM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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


graeme.
 
Old 12-07-2005, 08:42 AM   #7
vivekr
Member
 
Registered: Nov 2005
Location: Coimbatore,India
Distribution: Fedora Core4
Posts: 68

Original Poster
Rep: Reputation: 15
But the results are same for both. And the address of both are same and the arithmetics too operate the same way

Code:
    printf(" %u %u %u %u \n",b,c,b+1,c+1);
After adding this statement before the for loop:

Output:
 1245020 1245020 1245024 1245024
 1 1 2 2 3 3 4 4 5 5 6 6 7 7 0 0 0 0
Still wondering !?!
 
Old 12-07-2005, 12:02 PM   #8
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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.
 
Old 12-07-2005, 06:10 PM   #9
vivekr
Member
 
Registered: Nov 2005
Location: Coimbatore,India
Distribution: Fedora Core4
Posts: 68

Original Poster
Rep: Reputation: 15
Thanks to hivemind for his explanations.

For your remarks on the code--

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)

Last edited by vivekr; 12-07-2005 at 06:11 PM.
 
Old 12-08-2005, 10:20 PM   #10
mfrick
Member
 
Registered: Sep 2004
Location: Perth
Posts: 51

Rep: Reputation: 15
Quote:
Originally Posted by vivekr
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
pass pointer,, return pointer??? blizunt7 Programming 3 07-23-2005 01:36 PM
returning data to main() via a pointer to a pointer. slzckboy Programming 3 05-30-2005 01:20 PM
hot to set value of pointer to pointer to a structure in C alix123 Programming 2 11-17-2004 06:40 AM
pointer to pointer question in c lawkh Programming 2 01-29-2004 10:26 AM
gets(), pointer and the OS. bluesky Programming 4 02-04-2003 05:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration