FRIHOST • FORUMS • SEARCH • FAQ • TOS • BLOGS • COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


A C program in Linux





anooptdas
I tried the following program in RHEL 4 and got some unexpected results

main()
{
int a[2][2][2]={{15,2,3,4},{5,6,7,8}};
int *p,*q;
*q=***a;
printf("\n%d\n",*q);
printf("\n\n\n");
printf("%p a[0][0][0] %2d\n%p a[0][0][1] %2d\n%p a[0][1][0] %2d\n%p a[0][1][1] %2d\n%p a[1][0][0] %2d\n%p a[1][0][1] %2d\n%p a[1][1][0] %2d\n%p a[1][1][1] %2d\n",&a[0][0][0],a[0][0][0],&a[0][0][1],a[0][0][1],&a[0][1][0],a[0][1][0],&a[0][1][1],a[0][1][1],&a[1][0][0],a[1][0][0],&a[1][0][1],a[1][0][1],&a[1][1][0],a[1][1][0],&a[1][1][1],a[1][1][1]);
printf("\n\n\n");
}

When i print the array the value pointed by q gets printed as the third element.
Can any one please help
hexkid
anooptdas wrote:
I tried the following program in RHEL 4 and got some unexpected results

main()
{
int a[2][2][2]={{15,2,3,4},{5,6,7,8}};
int *p,*q;
*q=***a;
// ...
}

When i print the array the value pointed by q gets printed as the third element.
Can any one please help


`q` was never initialized. Try this:
Code:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  int a[2][2][2] = {{{15, 2}, {3, 4}}, {{5, 6}, {7, 8}}};
  int *q;

  q = malloc(sizeof *q);
  if (!q)
  {
    fprintf(stderr, "Not enough memory. Program aborted.");
    exit(EXIT_FAILURE);
  }

  *q = ***a;
  printf("\n%d\n", *q);
  return EXIT_SUCCESS;
}
anooptdas
thankyou

but y did the program behave strangely?
in windows it worked fine.
can u please explain the reason
hexkid
anooptdas wrote:
but y did the program behave strangely?
in windows it worked fine.
can u please explain the reason


It behaved strangely because you made it invoke undefined behaviour. You were lucky to not have demons flying out of your nose Smile

When UB (that's Undefined Behaviour) is invoked anything can happen, including the program behaving as you expect normally, but strangely when the client is testing it.

As for the technical reason it worked in Windows but not Linux, I can only guess: the initialization routine of your Windows compiler set the pointer to a valid read/write location (this time), but the Linux routine pointed it to a ROM location (and strangely [no, not really: anything can happen if you invoke UB] didn't core dump when you tried to write to it) and you get different results.
Related topics
C++ TUTORIAL : INTRODUCTION TO C++
What's wrong with this C code???
simple webserver on cable line.
A C program in Linux
C program with 3D array, in Linux
CLOSE PLEASE-Need a C++ program - upto 200frih$
[C] Splitting names program
Deploying a C# program
changing system time using a c program under cygwin
C++ Software
C++ Program Basics
C++ program yazmaya başlayalım artık :=)
Writing your first C++ Program
My Small C# Program
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.