what is wrong with this

F

free2cric

Hi,
I have declared pointer of pointers **a;
so In a loop I assign a block to a pointer and put a value in it
and then I want to print these values.

My following program doesnt work.

Also How to do same program with int *a[]; (array of pointers).
Any help would really help.

#include <stdio.h>
#include <conio.h>
#include <malloc.h>

void main()
{
int **a;
int v=0;

for (v=0;v<5;v++)
{
*(a+v)=(int *)malloc(sizeof(int));
**(a+v)=v;
}

for (v=0;v<5;v++)
{
printf("%d\n",**(a+v));
}

exit(0);

}
 
A

A. Sinan Unur

(e-mail address removed) wrote in @t39g2000cwt.googlegroups.com:
Any help would really help.

OK, then.
#include <stdio.h>
#include <conio.h>

conio.h is a non-standard header. Don't see any need for any nonstandard
constructs below, so omit this.
#include <malloc.h>

malloc.h is a non-standard header. You should:

#include <stdlib.h>

for malloc.
void main()

This is a non-standard prototype for main. In this case, you should use:

int main(void)
{
int **a;
int v=0;

for (v=0;v<5;v++)
{
*(a+v)=(int *)malloc(sizeof(int));

Don't cast the return value of malloc. It can hide failure to include
stdlib.h.
**(a+v)=v;

BAM! a itself points to nowhere. It should point to a chunk of memory
large enough to hold exactly five pointers to int.
}

for (v=0;v<5;v++)
{
printf("%d\n",**(a+v));
}

exit(0);

return 0;
}

is more conventional.

Sinan
 
P

pranav.choudhary

Perhaps i know where you have got this wrong. You declared
int **a;
can you read the above declaration, what does it say?? It says "a is a
pointer to pointer to int". So a is large enough to hold one pointer to
integer. But in your code you assume it large enough to hold 5 pointers
to integer, ie. a is not an array of 5 pointers to int, but just a
(single)pointer to pointer to int. To make your program work we have to
say

int **a;
a = (int **)malloc(5 *sizeof(int *));

I hope things are clear now.
 
A

A. Sinan Unur

Perhaps i know where you have got this wrong.

Who got what wrong? See said:
int **a;
a = (int **)malloc(5 *sizeof(int *));

#include <stdlib.h>

int **a = malloc(5 * sizeof(*a));

if ( a ) {

}

Casting the return value of malloc can hide errors due to failure to
include stdlib.h.

By using the form above, the malloc call remains the same even if you
change the type of a.
I hope things are clear now.

Not really.

Sinan
 
P

pranav.choudhary

I agree with that. My mail was not in reply to yours but to the
original mail. There was something else that was wrong with the posted
code and not the issue of not being able to include a header file. The
guy was simply treatin int **a; as int *a[5];
 
R

Richard Bos

I agree with that.

With _what_? Learn to tell Google Broken Beta to post with context, or
get a newsreader.
My mail was not in reply to yours but to the original mail.

There are no mails. There are only posts. Usenet is not an e-mail
service. Usenet is not the property of Google "do evil" Inc. Usenet is
an international network of newsgroups, with their own netiquette, far
preceeding the existence of Google. Learn to use it properly, please.

Richard
 
R

Richard G. Riley

Hi,
I have declared pointer of pointers **a;
so In a loop I assign a block to a pointer and put a value in it
and then I want to print these values.

My following program doesnt work.

Also How to do same program with int *a[]; (array of pointers).
Any help would really help.

#include <stdio.h>
#include <conio.h>
#include <malloc.h>

void main()
{
int **a;
int v=0;

for (v=0;v<5;v++)
{
*(a+v)=(int *)malloc(sizeof(int));

You have not yet allocated an area of memory to store your int
pointers. a is pointing to nothing valid.

There are other horrible things there too but if you get the
first bit right, the rest should logically follow as you get to grips
with pointers and pointers to pointers. Good luck!
 
C

CBFalconer

I have declared pointer of pointers **a;
so In a loop I assign a block to a pointer and put a value in it
and then I want to print these values.

My following program doesnt work.

Also How to do same program with int *a[]; (array of pointers).
Any help would really help.

#include <stdio.h>
#include <conio.h>

no such standard include file. Remove this.
#include <malloc.h>

No such standard include file. You probably want stdlib.h
void main()

All bets are off. main returns int. Say so.
{
int **a;
int v=0;

for (v=0;v<5;v++)

The use of blanks is allowed, and adds legibility. No extra charge
is made for them.

for (v = 0; v < 5; v++)
etc.
{
*(a+v)=(int *)malloc(sizeof(int));

Do not cast the return value of malloc. It only hides errors.
Besides, how can the left side, which has no associated memory,
hold the result? a has been defined to hold one single pointer to
a pointer to an int, and has never been initialized. Who knows
what (a + v) refers to. Even if the program somehow survived the
earlier gross errors, this made it go KA-BOOM. The approved way to
use malloc is:

ptr = malloc(SOMENUMBER * sizeof *ptr);

followed by checking for success, i.e. (ptr != NULL). Note that
the *s above are not the same thing, one is a multiplicative
operator, and one is a dereferencing operator.

In your case you would need multiple mallocs, something like:

if (a = malloc(5 * sizeof *a)) {
for (n = 0; n < 5; n++) {
if (*(a + n) = malloc(sizeof *(a + n))) {
/* now you have a place to store an int */
}
else handlemallocfailure();
}
else handlemallocfailure();

and somewhere there should be a define for the magic number 5.
**(a+v)=v;
}
for (v=0;v<5;v++)
{
printf("%d\n",**(a+v));
}
exit(0);
}

If you want to post a followup via groups.google.com, don't use the
broken "Reply" link at the bottom of the article. Click on "show
options" at the top of the article, then click on the "Reply" at
the bottom of the article headers.

More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>

--
Some informative links:
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
 
K

Keith Thompson

There are no mails. There are only posts. Usenet is not an e-mail
service. Usenet is not the property of Google "do evil" Inc. Usenet is
an international network of newsgroups, with their own netiquette, far
preceeding the existence of Google. Learn to use it properly, please.

.... by reading <http://cfaj.freeshell.org/google/>.
 
F

free2cric

ok guys whaterver u told , it helped me..
but who will free the allocated memory.. ur fathers????
 
F

Flash Gordon

(e-mail address removed) wrote:

Your response belongs *under* the text you are replying to, not above it.
ok guys whaterver u told , it helped me..

Please don't use contractions like "u" you "you" and "ur" for "your". It
makes it *far* harder for others to read what you are saying. In
general, since you want people to help you be nice to them by writing
properly or why should we put in the effort to solve your problems?
but who will free the allocated memory.. ur fathers????

<snip>

If you allocate memory it is up to you to deallocate it. That's why
there is a free function as well as malloc and realloc.
 
F

free2cric

Flash Gordon
Please don't use contractions like "u" you "you" and "ur" for "your". It
makes it *far* harder for others to read what you are saying.

I dont udenrstand why people can not understand "u" and "ur"
If you can understand C, C++ and complicated coding then its very easy
to understand
i which context "u" and "ur" is used. We are not writing the post to
mr. president to be so correct.

Thanks anyway for your feedback.

bye
cric.
 
V

Vladimir S. Oka

Flash Gordon


I dont udenrstand why people can not understand "u" and "ur"
If you can understand C, C++ and complicated coding then its very easy
to understand
i which context "u" and "ur" is used. We are not writing the post to
mr. president to be so correct.

I'm sure Mr. President wouldn't be happy at your lack of respect.

Silly phonetic abbreviations can be /very/ hard for non-native English
speakers to decode. I'm sure you do have some consideration (if not
respect) for them, don't you? Otherwise: Hau vud ju lajk if aj vrote in
vot aj konsider tu be fonetik ingliš?

Even for native or fluent English speakers they require extra effort to
decode (unless they learned it from age 3, I guess).

Also, /especially/ people who /really/ understand C (C++ is off-topic,
and not necessarily well understood in these parts), live and breathe
syntax correctness, and "u" and "ur" are certainly not correct English
syntax (also, look up "ur" in any German dictionary; C does not support
mixed language programming either).

Hope this clarifies this a little bit for you.

--
BR, Vladimir

From a certain point onward there is no longer any turning back.
That is the point that must be reached.
-- F. Kafka
 
W

websnarf

I have declared pointer of pointers **a;
so In a loop I assign a block to a pointer and put a value in it
and then I want to print these values.

My following program doesnt work.

Also How to do same program with int *a[]; (array of pointers).
Any help would really help.

#include <stdio.h>
#include <conio.h>
#include <malloc.h>

void main() {
int **a;
int v=0;

for (v=0;v<5;v++) {
*(a+v)=(int *)malloc(sizeof(int));
**(a+v)=v;
}

for (v=0;v<5;v++) {
printf("%d\n",**(a+v));
}
exit(0);
}

You are missing the "backbone" allocation. int ** a, is a pointer to
storage which itself points to additional storage for each integer.
You've allocated the storage for each integer, but not to the big
structure to hold these pointers.

If your compiler has good warnings, you could try to crank the warning
levels up and it should tell you that the line with *(a+v) = ... is
writing to the other end of (a+v) without that pointer having
initilizing it. It seems to me that you have to put something like:

a = (int **) malloc (5 * sizeof (int *));

at the beginning.

Remember that each * in your declaration requires you to point that
level at some storage (or to malloc it) directly in your code, while
each [] in your declaration implicitely declares its own storage for
that level (but may require you to calculate the sizeof() in mallocs at
higher levels carefully).
 
R

Richard Bos

Flash Gordon


I dont udenrstand why people can not understand "u" and "ur"
If you can understand C, C++ and complicated coding then its very easy
to understand
i which context "u" and "ur" is used. We are not writing the post to
mr. president to be so correct.

Congratulations: you are now officially too stupid to help.

Richard
 
K

Keith Thompson

Congratulations: you are now officially too stupid to help.

I suggest giving "free2cric" a break, at least tentatively. He admits
not understanding the problem; it's entirely possible that he's
capable of learning. (Even though he's been posting sporadically to
comp.lang.c for over a year, he might have missed the repeated
discussions of why silly abbreviations are a bad idea.)
 
R

Richard G. Riley

(e-mail address removed) wrote:
Silly phonetic abbreviations can be /very/ hard for non-native English
speakers to decode. I'm sure you do have some consideration (if not
respect) for them, don't you? Otherwise: Hau vud ju lajk if aj vrote in
vot aj konsider tu be fonetik ingli??

Even for native or fluent English speakers they require extra effort to
decode (unless they learned it from age 3, I guess).

Also, /especially/ people who /really/ understand C (C++ is
off-topic,

In many circles constant /hiliting/ is seen as obnoxious and
boring. It lights my newsreader like a beacon and for those that don't
support it, it breaks up the English. Please refrain from doing it so
frequently. It is really, really nasty.
 
F

Flash Gordon

Vladimir said:
I'm sure Mr. President wouldn't be happy at your lack of respect.

Silly phonetic abbreviations can be /very/ hard for non-native English
speakers to decode. I'm sure you do have some consideration (if not

<snip>

They are also very difficult for certain groups of *native* English
speakers, such as dyslexics (i.e. me). I, for example, will often have
to read a paragraph using those contractions at least twice before I
understand it, the second time much more slowly than normal. If they are
used more that a couple of times I might have to read it several times
because by the time I've decoded "ur" as "your" I've forgotten the rest
of the paragraph.

Given enough *years* of exposure, or months of *real* effort, it is
possible I might become fluent in such contractions. However, unless
someone is going to pay me a *lot* of money to do something that for me
is far harder than programming and which I do not enjoy at all, then I'm
not going to learn these contractions until they have been used in
normal English for several years.
 
A

August Karlstrom

Hi,
I have declared pointer of pointers **a;
so In a loop I assign a block to a pointer and put a value in it
and then I want to print these values.

My following program doesnt work.

Also How to do same program with int *a[]; (array of pointers).
Any help would really help.

#include <stdio.h>
#include <conio.h>
#include <malloc.h>

void main()
{
int **a;
int v=0;

for (v=0;v<5;v++)
{
*(a+v)=(int *)malloc(sizeof(int));
**(a+v)=v;
}

for (v=0;v<5;v++)
{
printf("%d\n",**(a+v));
}

exit(0);

}

You probably want something like the following.

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

#define LEN 5

int main(void)
{
int **a;
int k = 0;

a = malloc(sizeof *a);
a[0] = malloc(LEN * sizeof **a);
for (k = 0; k < LEN; k++) { a[0][k] = k; }
for (k = 0; k < LEN; k++) { printf("%d\n", a[0][k]); }
return 0;
}


August
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,946
Members
47,497
Latest member
PilarLumpk

Latest Threads

Top