473,785 Members | 2,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what is wrong with this

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);

}

Feb 24 '06 #1
27 1814
fr*******@yahoo .com wrote in news:1140783951 .891486.192110
@t39g2000cwt.go oglegroups.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
--
--
A. Sinan Unur <1u**@llenroc.u de.invalid>
(reverse each component and remove .invalid for email address)
Feb 24 '06 #2
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.

Feb 24 '06 #3
"pr************ **@gmail.com" <pr************ **@gmail.com> wrote in
news:11******** **************@ i40g2000cwc.goo glegroups.com:
Perhaps i know where you have got this wrong.
Who got what wrong? See <URL:http://cfaj.freeshell. org/google/>
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

--
A. Sinan Unur <1u**@llenroc.u de.invalid>
(reverse each component and remove .invalid for email address)
Feb 24 '06 #4
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];

Feb 24 '06 #5
"pr************ **@gmail.com" <pr************ **@gmail.com> wrote:
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
Feb 24 '06 #6
On 2006-02-24, fr*******@yahoo .com <fr*******@yaho o.com> wrote:
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!
**(a+v)=v;
}

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

exit(0);

}

--
Remove evomer to reply
Feb 24 '06 #7
fr*******@yahoo .com wrote:

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(SOMENUMB ER * 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 handlemallocfai lure();
}
else handlemallocfai lure();

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.c om, 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/googlegroupsrep ly/>

--
Some informative links:
news:news.annou nce.newusers
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
Feb 24 '06 #8
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
[...]
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/>.

--
Keith Thompson (The_Other_Keit h) 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.
Feb 24 '06 #9
ok guys whaterver u told , it helped me..
but who will free the allocated memory.. ur fathers????
Keith Thompson wrote:
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
[...]
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/>.

--
Keith Thompson (The_Other_Keit h) 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.


Feb 25 '06 #10

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

Similar topics

3
2384
by: Mike Henley | last post by:
I first came across rebol a while ago; it seemed interesting but then i was put off by its proprietary nature, although the core of the language is a free download. Recently however, i can't help but say i was totally impressed. I needed an open source wikiblog/wikilog, whatever you wanna call it, basically a hybrid of a blog and a wiki. I checked out snipsnap, which uses java, it was said on their site to be a clone of vanilla, a...
72
5898
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for example, between a C/C++ programmers with a few weeks of experience and a C/C++ programmer with years of experience. You don't really need to understand the subtle details or use the obscure features of either language
121
10180
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode support IDEs are DreamWeaver 8 and Zend PHP Studio. DreamWeaver provides full support for Unicode. However, DreamWeaver is a web editor rather than a PHP IDE. It only supports basic IntelliSense (or code completion) and doesn't have anything...
28
3279
by: Madhur | last post by:
Hello what about this nice way to open a file in single line rather than using if and else. #include<stdio.h> void main() { FILE *nd; clrscr(); fopen("c:\\autoexec.bat","r")&&printf("success") || printf("error opeing
9
2124
by: Pyenos | last post by:
import cPickle, shelve could someone tell me what things are wrong with my code? class progress: PROGRESS_TABLE_ACTIONS= DEFAULT_PROGRESS_DATA_FILE="progress_data" PROGRESS_OUTCOMES=
3
2149
by: Siong.Ong | last post by:
Dear all, my PHP aims to update a MySQL database by selecting record one by one and modify then save. Here are my PHP, but I found that it doesnt work as it supposed to be, for example, when Record (i) is shown and modified, the change will come to Record (i+1). Can anyone provide suggestion? thanks.
89
5771
by: Tubular Technician | last post by:
Hello, World! Reading this group for some time I came to the conclusion that people here are split into several fractions regarding size_t, including, but not limited to, * size_t is the right thing to use for every var that holds the number of or size in bytes of things. * size_t should only be used when dealing with library functions.
20
2821
by: Daniel.C | last post by:
Hello. I just copied this code from my book with no modification : #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc = 0;
24
3104
by: MU | last post by:
Hello I have some code that sets a dropdownlist control with a parameter from the querystring. However, when the querystring is empty, I get an error. Here is my code: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then
2
2235
by: mingke | last post by:
Hi... So I have problem with my if condition..I don't know what's wrong but it keeps resulting the wrong answer.... So here's the part of my code I have problem with: for (i=0; i<size2; i++){ for (k = 0; k < point3; k++){
0
9485
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10161
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10098
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9958
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8986
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7506
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3662
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.