473,811 Members | 2,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Does this look normal to you?

abc
Hello folks,

Does this qualify as normal behavior to you ?Compiled this with gcc 3.2

-----------------------------
#include <stdio.h>

static int array[2];

void initPointer(int * i_p)
{
i_p = array;
printf("INITP: pointer: %p\n",i_p);
};

int main (void)
{
printf("MAIN : array @: %p\n", array);

int * p = NULL;
initPointer(p);

printf("MAIN : pointer: %p\n",p);
}
---------------------------

The result ? Here we go :

MAIN : array @: 0x8049540
INITP: pointer: 0x8049540
MAIN : pointer: (nil) ---> What the heck is wrong ?
Nov 24 '05 #1
5 1162
On Thu, 24 Nov 2005 14:51:04 -0500, abc <no@spam.com> wrote:
Hello folks,

Does this qualify as normal behavior to you ?Compiled this with gcc 3.2

-----------------------------
#include <stdio.h>

static int array[2];

void initPointer(int * i_p)
{
i_p = array;
printf("INITP: pointer: %p\n",i_p);
};

int main (void)
{
printf("MAIN : array @: %p\n", array);

int * p = NULL;
initPointer(p);

printf("MAIN : pointer: %p\n",p);
}
---------------------------

The result ? Here we go :

MAIN : array @: 0x8049540
INITP: pointer: 0x8049540
MAIN : pointer: (nil) ---> What the heck is wrong ?


You're not changing p in main when you pass it to initPointer. You're
giving that function the value of the pointer, which it uses for the
i_p variable which is its parameter. If you want to change the actual
pointer in initPointer, pass it by reference.
Nov 24 '05 #2
abc wrote:
Hello folks,

Does this qualify as normal behavior to you ?Compiled this with gcc 3.2

-----------------------------
#include <stdio.h>

static int array[2];

void initPointer(int * i_p)
{
i_p = array;
printf("INITP: pointer: %p\n",i_p);
};

int main (void)
{
printf("MAIN : array @: %p\n", array);

int * p = NULL;
initPointer(p);

printf("MAIN : pointer: %p\n",p);
}
---------------------------

The result ? Here we go :

MAIN : array @: 0x8049540
INITP: pointer: 0x8049540
MAIN : pointer: (nil) ---> What the heck is wrong ?

Your function initPointer is taking the value of your pointer (ie. a
copy). To get this to work you need to use another level of indirection...

void initPointer(int ** i_p)
{
*i_p = array;
printf("INITP: pointer: %p\n",*i_p);
};

..
..

int * p = NULL;
initPointer(&p) ;

--dakka
Nov 24 '05 #3
abc wrote:
Hello folks,

Does this qualify as normal behavior to you ?Compiled this with gcc 3.2

-----------------------------
#include <stdio.h>

static int array[2];

void initPointer(int * i_p)
{
i_p = array;
printf("INITP: pointer: %p\n",i_p);
};

int main (void)
{
printf("MAIN : array @: %p\n", array);

int * p = NULL;
initPointer(p);

printf("MAIN : pointer: %p\n",p);
}
---------------------------

The result ? Here we go :

MAIN : array @: 0x8049540
INITP: pointer: 0x8049540
MAIN : pointer: (nil) ---> What the heck is wrong ?


Perfectly normal. Does this look normal to you?

void initInt(int i)
{
i = 2;
}

int main()
{
int i = 1;
initInt(i);
printf("%d\n", i); // prints 1 not 2
}

You have to realise that variables in one function are different from
variables in another function, even if they are pointers (why should
pointers be different?).

You need to use a reference or a pointer to change a variable in another
function. Because your variable is a pointer already that means you need
a refernce to a pointer or a pointer to a pointer.

john
Nov 24 '05 #4
On Thu, 24 Nov 2005 14:51:04 -0500, abc <no@spam.com> wrote in
comp.lang.c++:
Hello folks,

Does this qualify as normal behavior to you ?Compiled this with gcc 3.2

-----------------------------
#include <stdio.h>

static int array[2];

void initPointer(int * i_p)
{
i_p = array;
printf("INITP: pointer: %p\n",i_p);
In addition to what the others have pointed out, this is undefined
behavior. The "%p" conversion specifier to printf() requires a
pointer to void, not pointer to int or to any other type. Should be:

printf("INITP: pointer: %p\n", (void *)i_p);

Of course C++ stream insertion operators handle any type of pointer,
with the exception of pointer to char, without any cast.
};

int main (void)
{
printf("MAIN : array @: %p\n", array);
Same here.

int * p = NULL;
initPointer(p);

printf("MAIN : pointer: %p\n",p);
And here.
}


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 25 '05 #5

"abc" <no@spam.com> wrote in message
news:dm******** **@zcars129.ca. nortel.com...
| Hello folks,
|
| Does this qualify as normal behavior to you ?Compiled this with gcc
3.2
|
| -----------------------------
| #include <stdio.h>
|
| static int array[2];
|
| void initPointer(int * i_p)
| {
| i_p = array;
| printf("INITP: pointer: %p\n",i_p);
| };

redundant semicolon above

|
| int main (void)
| {
| printf("MAIN : array @: %p\n", array);
|
| int * p = NULL;
| initPointer(p);
|
| printf("MAIN : pointer: %p\n",p);
| }
| ---------------------------
|
| The result ? Here we go :
|
| MAIN : array @: 0x8049540
| INITP: pointer: 0x8049540
| MAIN : pointer: (nil) ---> What the heck is wrong ?
|

Think about it.
You are passing a null pointer as a parameter to initPointer(... ) which
sets i_p to 0 momentarily.
You then modify the local i_p pointer to &array[0], a valid pointer.
Why should p, who's on another planet, not be null?

You might as well have written:

void initPointer()
{
int *i_p = array;
printf("INITP: pointer: %p\n",i_p);
}

which is probably what your compiler did when it optimized the code.
Try something like this instead:

static int array[2];

int main()
{
int *p = &array[0];
}
Nov 25 '05 #6

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

Similar topics

14
4340
by: billy.becker | last post by:
I need to save a wav file that is HTTP POSTed to a php page. What does PHP5 do automatically to a POSTed variable when it puts it in the $_POST superglobal array? I haven't been able to find any info on this.... I have a script that does what I want in PERL, but I need to do it in PHP. I think the PERL does something magic when it does this:
7
19501
by: Fabian Neumann | last post by:
Hi! I got a problem with font-family inheritance. Let's say I have CSS definitions like: p { font:normal 10pt Verdana; } strong { font:normal 14pt inherit;
19
9296
by: deko | last post by:
Firefox will not take the following "font-weight:bold" directive in my stylesheet. Works fine in IE. #rightMenuText h5 { font-weight:bold; padding-bottom:0px; padding-top: 10px; margin-bottom:0px; }
8
3039
by: Jaime Rios | last post by:
Hi, I created a COM AddIn for Word that performs the functions that it needs to, but I needed to add the ability for the toolbar created by the COM AddIn to remember it's last position and whether it was docked or not. I added the following code to my "OnConnection" function but it fails with an error, "Run-time exception thrown : System.IO.IOException - Bad file name or number." With applicationObject.CommandBars("SampleToolbar")
2
2280
by: Wysiwyg | last post by:
I am using MS Visual Studio 2002 with Windows 2000 Advanced Server and am starting to learn C#. Perhaps this is obvious but I can't see if what's happening is "normal" or if something needs to be set differently. I have implemented System.Configuration.NameValueSectionHandler which is found in System.dll. The problem I have is this only work if I either manually copy System.dll to the application's bin directory or set the System reference...
1
3476
by: Wendy Elizabeth | last post by:
Can you give me some suggestions of why the xml web service is not working? I have an xml web service that works in my visual studio. net 1.1 environment. I setup this project up for deployment doing the following steps: 1. add a web setup project called "testaddr". 2. in file system window, select Web Application folder. 3. in the left pane of the file system window, right-click web application, point to add, and then click output...
16
4992
by: lawrence k | last post by:
I've a file upload script on my site. I just now used it to upload a small text document (10k). Everything worked fine. Then I tried to upload a 5.3 meg Quicktime video. Didn't work. I've set the POST limit in php.ini to 8 megs. What reasons, other than the POST limit, would a large upload fail?
14
4867
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it can be done using the designer but I intentionally don't want to use that. The one reason is that you cannot change the code generated by the designer. The other could be that you have more free hand and control to design your GUI. 2....
13
10561
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, Why does Math.Sqrt() only accept a double as a parameter? I would think it would be just as happy with a decimal (or int, or float, or ....). I can easily convert back and forth, but I am interested in what is going on behind the scenes and if there is some aspect of decimals that keep them from being used in this calculation. Thanks! Ethan Ethan Strauss Ph.D.
0
9603
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
10379
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...
0
10124
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
9200
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
7664
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
5550
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...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4334
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.