473,396 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Really basic..

if i define like,

float **data;

then can't i do this in my programm..

data[0][0]=23.23;

why this is alway giving some kind of memory error..

#include "stdio.h"
int main()
{
float **data;
data[0][0]=23.23;
return 0;

}

am i missing something fudamental thing...

thanks in advance.

Jun 13 '06 #1
18 1585

deepak wrote:
if i define like,

float **data;

then can't i do this in my programm..

data[0][0]=23.23;

why this is alway giving some kind of memory error..

#include "stdio.h"
int main()
{
float **data;
data[0][0]=23.23;
return 0;

}

am i missing something fudamental thing...


Yes. You're failing to allocate any memory for your data.

float **data;

only declares a pointer to where your data is, no memory is allocated.

Try:

#include <stdlib.h>
...

data = malloc( N_ROWS * N_COLS * sizeof **data);

if (data == NULL) /* do something about the error */;
...

Jun 13 '06 #2

Vladimir Oka wrote:
deepak wrote:
if i define like,

float **data;

then can't i do this in my programm..

data[0][0]=23.23;

why this is alway giving some kind of memory error..

#include "stdio.h"
int main()
{
float **data;
data[0][0]=23.23;
return 0;

}

am i missing something fudamental thing...


Yes. You're failing to allocate any memory for your data.

float **data;

only declares a pointer to where your data is, no memory is allocated.

Try:

#include <stdlib.h>
...

data = malloc( N_ROWS * N_COLS * sizeof **data);

if (data == NULL) /* do something about the error */;
...


Obviously, you'd need to `#define` N_ROWS and N_COLS as well.

Jun 13 '06 #3
deepak wrote:
if i define like,

float **data;

then can't i do this in my programm..

data[0][0]=23.23;


You've said "the variable `data` may hold a value which is a pointer to
pointer to float". You haven't (even in the snipped code) put such a
value into it. BOOM today.

You need to read up on `malloc` and pointers.

--
Chris "seeker" Dollin
"Reaching out for mirrors hidden in the web." - Renaissance, /Running Hard/

Jun 13 '06 #4
Le 13-06-2006, Vladimir Oka <no****@btopenworld.com> a écrit*:

deepak wrote:
if i define like,

float **data;

then can't i do this in my programm..

data[0][0]=23.23;

why this is alway giving some kind of memory error..

#include "stdio.h"
int main()
{
float **data;
data[0][0]=23.23;
return 0;

}

am i missing something fudamental thing...


Yes. You're failing to allocate any memory for your data.

float **data;

only declares a pointer to where your data is, no memory is allocated.

Try:

#include <stdlib.h>
...

data = malloc( N_ROWS * N_COLS * sizeof **data);


The problement of 2d array is not in the FAQ ?
With this malloc, you create a 1 flat dimensionnal array of N_ROWS *
N_COLS float.

But, notation data[0][0] assumes that data[0] is a pointer...

Marc Boyer
Jun 13 '06 #5

Marc Boyer wrote:
Le 13-06-2006, Vladimir Oka <no****@btopenworld.com> a écrit :

deepak wrote:
if i define like,

float **data;

then can't i do this in my programm..

data[0][0]=23.23;

why this is alway giving some kind of memory error..

#include "stdio.h"
int main()
{
float **data;
data[0][0]=23.23;
return 0;

}

am i missing something fudamental thing...
Yes. You're failing to allocate any memory for your data.

float **data;

only declares a pointer to where your data is, no memory is allocated.

Try:

#include <stdlib.h>
...

data = malloc( N_ROWS * N_COLS * sizeof **data);


The problement of 2d array is not in the FAQ ?


Yes it is: 6.16 <http://c-faq.com/aryptr/dynmuldimary.html>.
With this malloc, you create a 1 flat dimensionnal array of N_ROWS *
N_COLS float.

But, notation data[0][0] assumes that data[0] is a pointer...


Yes, I have bungled it.

I had a contiguous array in mind. From the FAQ:

int **array2 = malloc(nrows * sizeof(int *));
array2[0] = malloc(nrows * ncolumns * sizeof(int));
for(i = 1; i < nrows; i++)
array2[i] = array2[0] + i * ncolumns;

Jun 13 '06 #6
deepak posted:
if i define like,

float **data;

then can't i do this in my programm..

data[0][0]=23.23;

Firstly, specify the amount of rows and columns:

enum { cols = 8, rows = 8 };
Then choose either stack allocation:

float chessboard[cols][rows];

chessboard[0][0] = 23.23;
Or dynamic:

float (*chessboard)[rows] = malloc( sizeof( float[cols][rows] ) );

chessboard[0][0] = 23.45;

--

Frederick Gotham
Jun 17 '06 #7
Frederick Gotham said:
float (*chessboard)[rows] = malloc( sizeof( float[cols][rows] ) );
Better argument to malloc: sizeof *chessboard
chessboard[0][0] = 23.45;


Surely you're not going to do that before checking that chessboard != NULL?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 17 '06 #8
Frederick Gotham wrote:
deepak posted:
if i define like,

float **data;

then can't i do this in my programm..

data[0][0]=23.23;

Firstly, specify the amount of rows and columns:

enum { cols = 8, rows = 8 };


A slight misuse of the enum construct here, I think. The following is
probably clearer:

#define ROWS 8
#define COLS 8
Then choose either stack allocation:

float chessboard[cols][rows];

chessboard[0][0] = 23.23;
Or dynamic:

float (*chessboard)[rows] = malloc( sizeof( float[cols][rows] ) );

chessboard[0][0] = 23.45;

August
Jun 18 '06 #9
August Karlstrom wrote:
Frederick Gotham wrote:
deepak posted:
if i define like,

float **data;

then can't i do this in my programm..

data[0][0]=23.23;


Firstly, specify the amount of rows and columns:

enum { cols = 8, rows = 8 };

A slight misuse of the enum construct here, I think. The following is
probably clearer:

#define ROWS 8
#define COLS 8

Not realy, if some poor soul is debugging the code and wants to know the
value of rows, he'll have to find the #define, whereas with and enum, he
can check the value with his debugger.

--
Ian Collins.
Jun 18 '06 #10
August Karlstrom posted:

A slight misuse of the enum construct here, I think. The following is
probably clearer:

#define ROWS 8
#define COLS 8


I don't know what the general consenus is in C, but coming from my
background of C++, macros are to be abhorred wherever an "enum" or
"typedef" can get the job done. There are many reasons why... here's just
one:

#define ROWS 8
#define COLS 8

int ArbitraryFunc( int ROWS, int COLS )
{
/* ... */
}

--

Frederick Gotham
Jun 18 '06 #11
On Sat, 17 Jun 2006 19:03:01 GMT, Frederick Gotham
<fg*******@SPAM.com> wrote:
deepak posted:
if i define like,

float **data;

then can't i do this in my programm..

data[0][0]=23.23;

Firstly, specify the amount of rows and columns:

enum { cols = 8, rows = 8 };
Then choose either stack allocation:


C doesn't require a stack.

float chessboard[cols][rows];

chessboard[0][0] = 23.23;
Or dynamic:

float (*chessboard)[rows] = malloc( sizeof( float[cols][rows] ) );
This allocates too much memory. You want
float (*chessboard)[rows] = malloc(cols * sizeof (float[rows]);
or the equivalent but much preferred
float (*chessboard)[rows] = malloc(cols * sizeof *chessboard);

chessboard[0][0] = 23.45;

Remove del for email
Jun 18 '06 #12
On 13 Jun 2006 01:44:39 -0700, "deepak" <dk*******@gmail.com> wrote:
if i define like,

float **data;

then can't i do this in my programm..

data[0][0]=23.23;

why this is alway giving some kind of memory error..

#include "stdio.h"
int main()
{
float **data;
data[0][0]=23.23;
You cannot dereference a pointer (which is what the [] operator does)
until you have initialized/assigned the pointer to point to some
memory you actually own. Both data and data[0] are uninitialized
pointers you have to deal with.
return 0;

}

am i missing something fudamental thing...

thanks in advance.

Remove del for email
Jun 18 '06 #13
Frederick Gotham said:
August Karlstrom posted:

A slight misuse of the enum construct here, I think. The following is
probably clearer:

#define ROWS 8
#define COLS 8


I don't know what the general consenus is in C, but coming from my
background of C++, macros are to be abhorred wherever an "enum" or
"typedef" can get the job done. There are many reasons why... here's just
one:

#define ROWS 8
#define COLS 8

int ArbitraryFunc( int ROWS, int COLS )
{
/* ... */
}


It is fairly widely accepted in C circles that objects and functions should
be given mixed or lower case identifiers, so in practice the above problem
tends not to arise. I would have no hesitation in using the #defines given
above, in a C program. In C++, I'd probably use a const int, because in C++
that is the idiom. But this is not a C++ newsgroup.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 18 '06 #14
On Sat, 17 Jun 2006 18:38:39 -0700, Barry Schwarz <sc******@doezl.net>
wrote:
On Sat, 17 Jun 2006 19:03:01 GMT, Frederick Gotham
<fg*******@SPAM.com> wrote:
deepak posted:
if i define like,

float **data;

then can't i do this in my programm..

data[0][0]=23.23;

Firstly, specify the amount of rows and columns:

enum { cols = 8, rows = 8 };
Then choose either stack allocation:


C doesn't require a stack.

float chessboard[cols][rows];

chessboard[0][0] = 23.23;
Or dynamic:

float (*chessboard)[rows] = malloc( sizeof( float[cols][rows] ) );


This allocates too much memory. You want
float (*chessboard)[rows] = malloc(cols * sizeof (float[rows]);
or the equivalent but much preferred
float (*chessboard)[rows] = malloc(cols * sizeof *chessboard);


Disregard. Jetlag leads to erroneous posts..

chessboard[0][0] = 23.45;

Remove del for email

Remove del for email
Jun 18 '06 #15
Frederick Gotham wrote:
August Karlstrom posted:

A slight misuse of the enum construct here, I think. The following is
probably clearer:

#define ROWS 8
#define COLS 8


I don't know what the general consenus is in C, but coming from my
background of C++, macros are to be abhorred wherever an "enum" or
"typedef" can get the job done. There are many reasons why... here's just
one:

#define ROWS 8
#define COLS 8

int ArbitraryFunc( int ROWS, int COLS )
{
/* ... */
}

As Richard mentions further down the thread, using all caps for formal
parameters is bad practice.

A variable of an enumerated type holds a "symbol" (C as opposed to e.g.
Lisp don't have "real" symbols). The value of the "symbol" is (usually)
not important to the client. A typical example is

enum animal {CAT, DOG, BIRD};
...
enum animal x = BIRD;

If you aim for clarity, my suggestion to use `#define' is preferable.

Hint: If you were to give your anonymous type

enum { cols = 8, rows = 8 };

a name, what would that be? Moreover, e.g.

enum foo { cols = 8, rows = 8};
...
enum foo x = rows;

doesn't make sense.
August
Jun 20 '06 #16
August Karlstrom posted:
Hint: If you were to give your anonymous type

enum { cols = 8, rows = 8 };

a name, what would that be? Moreover, e.g.

enum foo { cols = 8, rows = 8};
...
enum foo x = rows;

doesn't make sense.


I would choose ANYTHING over a macro, e.g.:

unsigned const rows = 8U;
unsigned const cols = 8U;
typedef unsigned Dimension;

Or:

enum Dimension { rows = 8U, cols = 8U };

I must admit that I don't understand why macros are not seen as as
respulsive in the general C community, as they are in the general C++
community.
--

Frederick Gotham
Jun 20 '06 #17
August Karlstrom <fu********@comhem.se> writes:
[...]
A variable of an enumerated type holds a "symbol" (C as opposed to
e.g. Lisp don't have "real" symbols). The value of the "symbol" is
(usually) not important to the client. A typical example is

enum animal {CAT, DOG, BIRD};
...
enum animal x = BIRD;

If you aim for clarity, my suggestion to use `#define' is preferable.

Hint: If you were to give your anonymous type

enum { cols = 8, rows = 8 };

a name, what would that be? Moreover, e.g.

enum foo { cols = 8, rows = 8};
...
enum foo x = rows;

doesn't make sense.


Actually, using an enum declaration to declare named constants is a
fairly common idiom. It's arguably an abuse of the feature, but it's
also the best way to create a symbolic constant (at least one of type
int). A macro, though it's perfectly usable with care, brings in all
the problems of the preprocessor (lack of scope, counterintuitive
results in expressions, etc). A const declaration creates a read-only
variable, not a true constant.

So for the above, I might write:

enum { COLS = 8 };
enum { ROWS = 8 };

By separating COLS and ROWS into two separate declarations, this
avoids giving the impression that I'm creating a meaningful type.
Putting the names in all-caps makes it clear that they're constants.
And to answer your question, I wouldn't give such a type a name;
creating a type is just a side effect of what's intended to be a
constant declaration.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 20 '06 #18
Frederick Gotham <fg*******@SPAM.com> wrote:
I would choose ANYTHING over a macro, e.g.:

unsigned const rows = 8U;
unsigned const cols = 8U;
Whyever the Us after the constants? It's not as if it changes anything.
typedef unsigned Dimension;

Or:

enum Dimension { rows = 8U, cols = 8U };

I must admit that I don't understand why macros are not seen as as
respulsive in the general C community, as they are in the general C++
community.


Possibly because C was created to get the job done properly, rather than
to prove a point.

Richard
Jun 21 '06 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Simo | last post by:
Hi all, ok i'm brand new to css. I was investigating it to make my site more versatile. At the moment it is entirely flash. I want to make 2 versions of it. one plain text html and the other...
5
by: Rank Novice | last post by:
I'm a developer, not a DB admin. I'm writing a .NET app that uses crystal reports. The table I need to output is built inside a stored procedure. No choice, it makes use of some temporary...
2
by: JohnFol | last post by:
I have followed one of the walkthroughs that shows a parameterised query populating some text boxes that are data bound to a data set. The full details are found at ...
2
by: Tommy Vercetti | last post by:
In Visual Studio .NET 2003, I do the following 1) File->New->Project 2) Visual C++ Projects->.NET->Windows Forms Application 3) Project->Add New Item->C++ File 4) Clear the new C++ file and...
2
by: jack | last post by:
hi all where on net can i find basics of regex all i was able to find was some of the complex things please help
1
by: bruce | last post by:
hi... i have the following test python script.... i'm trying to figure out a couple of things... 1st.. how can i write the output of the "label" to an array, and then how i can select a given...
8
by: =?Utf-8?B?TFc=?= | last post by:
Hello! I am just learning about forms authentication so please excuse this basic question. I am using .NET 1.1 and C#. I have created my web.config file and my login.aspx and the associated cs...
4
by: tomamil | last post by:
i know this example is stupid and useless, but that's not the answer to my question. here it goes: status = 0.0 for i in range(10): status = status + 0.1 if status == 0.1: print status
1
by: wildman | last post by:
It's been over 10 years that I don't look at Access. I've been working in asp.net for the last 5 developing intranet sites. Just got a project where we are trying to avoid building an...
27
by: Mitch | last post by:
I'm brand new, and just started C# for Dummies. I've also put together a list of websites with code snippets and tutorials. If I cut-and-paste the following line from an example into Visual...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.