linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* RE: complex variable
@ 2004-09-08 18:40 Huber, George K RDECOM CERDEC STCD SRI
  2004-09-10 20:09 ` Jan-Benedict Glaw
  0 siblings, 1 reply; 14+ messages in thread
From: Huber, George K RDECOM CERDEC STCD SRI @ 2004-09-08 18:40 UTC (permalink / raw)
  To: linux-c-programming

Hi,

First (for Charlie), I attempted to keep as much of Ankit's code consistent 
from his posting to my sample code - including unused variables,
non-portable 
constructs, use of N instead of a #define 

As both Charlie and Jan-Benedict both noted doing an declaration and
defination
of the pointers is not necessary - I plead old habits developed when working

with various microsoft compilers.  What can I say - old habits die hard.

Now for Ankits question:

>but sorry sir i could not understand one thing which u
>also clearly detected. it is "in" variable....its a
>complex variable. this is somewhat not correct

>complex x, *in = NULL;

>but i wanted to ask if i want to use in as a 2D array
>how to do that?

Assuming that you want to create a two dimensional array of complex types
(with
the added assumption that number of rows is not known at compile time), I
would
do something along these lines.  To create a dynamic two-dimensional array,
you 
allocate enough memory to hold all of the elements and manage the space
yourself.
For example, if you wanted to create a 3 rows by 2 column array you would
need 
to allocate space for six elements.  Because C lays out it arrays in
row-major
for, the space would be:

   +-----+-----+-----+-----+-----+-----+
   | 0,0 | 0,1 | 1,0 | 1,1 | 2,0 | 2,1 |
   +-----+-----+-----+-----+-----+-----+
      0     1     2     3     4     5

with the corresponding index shown below.  To make life easy, you need to
create
a function that will take the a two-dimensional index, i.e. (row, col), and
convert 
it to the corresponding one-dimensional index.  I tend to use a macro to do
this,

#define MakeIndex(r,c)  (r)*NUM_COLS + (y)

Now if I wanted to create the above two-dimensional array, I would allocate
space
for it like this (I have used fixed values for number of columns and number
of 
rows for demonstration purposes only.  If these values were set at compile
time it 
would be easier to do complex in[3][2])
	
   complex*   in;
   int        nRows = 3;
   int        nCols = 2;

   in = malloc(sizeof(complex)*3*2);

Now having created a region in memory to hold by two dimensional array we
can do things
like:

   complex    var1, var2;
   in[MakeIndex(0,0)] = var1;    // assign a value into the 2D array at row
0 column 0
   var2 = in[MakeIndex(1,1)];    // access a vlue in the 2D array at row 1
column 1

Note that error checking has not been performed.  It is up to the
application to insure 
that bounds are not overstepped.  For example, element (1,2) does not exist
as this would 
imply the array having three columns.

As I said earlier, if both the number of rows and number of columns are know
at compile 
time things become much easier.  You can use the normal square-bracket
notation to access
elements.

Hope this helps
George


   

^ permalink raw reply	[flat|nested] 14+ messages in thread
* RE: complex variable
@ 2004-09-07 15:37 Huber, George K RDECOM CERDEC STCD SRI
  2004-09-07 16:05 ` Jan-Benedict Glaw
  2004-09-08  9:59 ` Charlie Gordon
  0 siblings, 2 replies; 14+ messages in thread
From: Huber, George K RDECOM CERDEC STCD SRI @ 2004-09-07 15:37 UTC (permalink / raw)
  To: linux-c-programming


>       1 #include<stdlib.h>
>       2 #include<complex.h>
>       3 #include<stdio.h>
>       4
>       5 int main()
>       6 {
>       7         FILE *fp;
>       8         fp=fopen("test_data.dat","rb");
>       9         int N=32,i=0;
>      10         complex x,*in;
>      11         in=malloc(sizeof(complex)*N);
>      12         if (fread(in,16,32,fp) != 32)
>      13          printf("error in reading from file
> failed\n");
>      14         else{
>      15         for(i=0;i<N;i++)
>      16           printf("%lf\t",*(in+i+0));
>      17         }
>      18         return 0;
>      19 }
> 
> i am sorry but this code also gives the same error
> 
Hi,

In addition to the previously given suggestions, I would suggest checking
the return 
value of fopen (it can fail), as well as printing out the error codes.  For
example,
I would recode the above as (note - this is uncompiled/tested code but is
should
give an example of what I mean):

#include<stdlib.h>
#include<complex.h>
#include<stdio.h>
#include<errno.h>

int main()
{
    FILE* fp=NULL;
    char  szName[] = "test_data.dat";

    if(NULL != (fp = fopen(szName, "rb")))
    {
        int N=32, i=0;
        complex x, *in = NULL;
	  if(NULL != (in=malloc(sizeof(complex)*N)))
        {
            if(N == fread(in, sizeof(complex), N, fp))
            {
                for(i=0; i< N; i++)
                    printf("%lf\t", in[i]);
            }
            else
            {
                fprintf(stderr, "Read failed. error: %d\n%s\n", errno,
strerror(errno));
            }
        }
        else
        {
            fprintf(stderr, "Failed to allocate space for buffer\n");
        }
    }
    else
    {
        fprintf(stderr, "Failed to open %s. Error %d\n%s\n", 
                szName, errno, strerror(errno));
    }

    return 0;
}   

^ permalink raw reply	[flat|nested] 14+ messages in thread
[parent not found: <20040906134525.33692.qmail@web52908.mail.yahoo.com>]

end of thread, other threads:[~2004-09-14  8:16 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-09-08 18:40 complex variable Huber, George K RDECOM CERDEC STCD SRI
2004-09-10 20:09 ` Jan-Benedict Glaw
  -- strict thread matches above, loose matches on Subject: below --
2004-09-07 15:37 Huber, George K RDECOM CERDEC STCD SRI
2004-09-07 16:05 ` Jan-Benedict Glaw
2004-09-08  9:59 ` Charlie Gordon
2004-09-08 12:06   ` Jan-Benedict Glaw
2004-09-08 16:25     ` Ankit Jain
2004-09-08 21:54     ` Charlie Gordon
2004-09-10 20:05       ` Jan-Benedict Glaw
2004-09-13 19:32         ` Charlie Gordon
2004-09-13 21:57           ` Jan-Benedict Glaw
2004-09-14  8:16             ` Charlie Gordon
     [not found] <20040906134525.33692.qmail@web52908.mail.yahoo.com>
2004-09-06 16:25 ` Ankit Jain
2004-09-06 16:37   ` Jan-Benedict Glaw

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).