473,651 Members | 2,582 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How is it working? Am I doing it right?

Weird subject you will say.. I didn't know what to put in there..

Long story short, I'm changing the value of a variable from a
function.. Nothing unusual there..

How come the value of the "word" variable change when using the
testfunct function? Am I sending a pointer to the word variable to
this function or whatever ??

I know it's a weird question.. especially that this is doing exactly
what I need.. but I want to know if I'm doing it right and if not, how
to make it work correctly.

The program where this is going is checking a configuration text file
and puts all the lines in a 2d char array for comparison later. Here
is a simplified "extraction " of the code.

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

int testfunct (char *myword)
{
strcpy(myword," hello");

return 0;
}

int main (int argc, char *argv[])
{
char word[100];

strcpy(word,"we eee");
printf("WORD: %s\n", word);
testfunct(word) ;
printf("WORD: %s\n", word);

return 0;
}
------
Jun 27 '08 #1
4 1278
In article <d0************ *************** *******@27g2000 hsf.googlegroup s.com>,
Benoit Lefebvre <be************ *@gmail.comwrot e:
>How come the value of the "word" variable change when using the
testfunct function? Am I sending a pointer to the word variable to
this function or whatever ??
>int testfunct (char *myword)
{
strcpy(myword," hello");

return 0;
}
>int main (int argc, char *argv[])
{
char word[100];

strcpy(word,"we eee");
printf("WORD: %s\n", word);
testfunct(word) ;
When you pass an array name as a function parameter, it gets silently
converted into a pointer to the first element of the array.
printf("WORD: %s\n", word);

return 0;
}
So yes, the above is fine / legal / well-defined, as long as testfunct()
does not write more characters into myword than the object is defined
to hold.
--
"The art of storytelling is reaching its end because the epic
side of truth, wisdom, is dying out." -- Walter Benjamin
Jun 27 '08 #2
Benoit Lefebvre <be************ *@gmail.comwrit es:
Weird subject you will say.. I didn't know what to put in there..

Long story short, I'm changing the value of a variable from a
function.. Nothing unusual there..

How come the value of the "word" variable change when using the
testfunct function? Am I sending a pointer to the word variable to
this function or whatever ??

I know it's a weird question.. especially that this is doing exactly
what I need.. but I want to know if I'm doing it right and if not, how
to make it work correctly.

The program where this is going is checking a configuration text file
and puts all the lines in a 2d char array for comparison later. Here
is a simplified "extraction " of the code.

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

int testfunct (char *myword)
{
strcpy(myword," hello");

return 0;
}

int main (int argc, char *argv[])
{
char word[100];

strcpy(word,"we eee");
printf("WORD: %s\n", word);
testfunct(word) ;
printf("WORD: %s\n", word);

return 0;
}
Yes, you're sending a pointer to the function.

``word'' is an array object, so the argument to testfunct is an
expression of array type, which is implicitly converted to a pointer
to the array's first element. testfunct() then modifies the data that
its argument points to.

Read section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #3
ro******@ibd.nr c-cnrc.gc.ca (Walter Roberson) writes:
[...]
When you pass an array name as a function parameter, it gets silently
converted into a pointer to the first element of the array.
[...]

Yes. This is, of course, just one instance of a more general rule
having nothing to do with functions.

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #4
Benoit Lefebvre wrote:
Weird subject you will say.. I didn't know what to put in there..

Long story short, I'm changing the value of a variable from a
function.. Nothing unusual there..

How come the value of the "word" variable change when using the
testfunct function? Am I sending a pointer to the word variable to
this function or whatever ??

I know it's a weird question.. especially that this is doing exactly
what I need.. but I want to know if I'm doing it right and if not, how
to make it work correctly.

The program where this is going is checking a configuration text file
and puts all the lines in a 2d char array for comparison later. Here
is a simplified "extraction " of the code.

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

int testfunct (char *myword)
testfunct takes as a parameter a character pointer. A pointer to a
character or array of characters depending on how you use it.
{
strcpy(myword," hello");
strcpy will copy into the location of the character pointer the contents of
the 2nd parameter.
>
return 0;
}

int main (int argc, char *argv[])
{
char word[100];
word is defined as an array of 100 characters. &word[0] is the address of
the first character, but so is word by itself. It gets silently converted.
>
strcpy(word,"we eee");
here word is silently converted to a char pointer, the same as &word[0]
which is where strcpy will start copying the contents of the 2nd parameter.
printf("WORD: %s\n", word);
testfunct(word) ;
here testfunct takes a char pointer so the compiler silently converts word
to a char pointer, the same as &word[0]
printf("WORD: %s\n", word);

return 0;
}
------
It is a well formed program.

Note, however, that you may wish to pass a 2nd parameter to your function,
the size of the buffer.

If you had done:
char word[2];
testfunct(word) ;

The compiler would accept it and when you ran it you would get a buffer
overflow because strcpy doesn't know how long the buffer the char pointer is
pointing to, it has no way to know. So it mindlessly copies until it
reaches the terminating null character in the copy from buffer, effectively
trying to stuff 10 pounds of stuff in a 5 pound bag.
--
Jim Langston
ta*******@rocke tmail.com
Jun 27 '08 #5

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

Similar topics

0
1895
by: Don Leckie | last post by:
Hi, I'm upgrading our software from JDK 1.3.1_03 to JDK 1.4.2_07 and I'm having trouble with ComponentOrientation.RIGHT_TO_LEFT working correctly in a JScrollPane. One of my windows has a JSplitPane. There is a JScrollPane in each side. There's a JTable in each scroll pane. The left scroll pane has the vertical JScrollBar on the left side. The right scroll pane has the veritcal JScrollBar on the right side (default).
8
3470
by: tshad | last post by:
I cannot seem to get the asp:textbox to use classes. Style works fine. I am trying to set the textbox to act like a label in some instance so it doesn't have a border, readonly and the background is grey. I have a class set as: ..table2Label{ border-style:none; background-color:#F6F6F6; }
11
1854
by: tshad | last post by:
I have a W2003 server running my website and I am trying to set up my pages to send email using System.Web.Mail. I have pages running on this machine using CDONTS that work fine. I am using our exchange server to send the mail but am getting an "Unable to relay" message. But the "From" name is my email address, which is obviously on our exchange server, so there is no relay. The message I get back is:
132
4573
by: Frederick Gotham | last post by:
If we look at a programming language such as C++: When an updated Standard comes out, everyone adopts it and abandons the previous one. It seems though that things aren't so clear-cut in the C community. It would seem that C99 is the most up-to-date Standard, but far more people seem to be working off the C89 Standard. Could someone please explain to me why this is so? --
8
1466
by: mehstg1319 | last post by:
Hi there Am doing some work on a page at the moment, a bit of a newbie at the moment with CSS and am having a few troubles. The left hand navigation does not seem to work when a gap is inserted in the space between it and the body text. I could not get left margin or left padding to work. So in the end, I positioned the bodytxt div as relative and put left:10px; in to give a 10px gap. When this gap is removed, the left nav works fine,...
0
8347
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8275
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
8694
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
8457
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
5605
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
4143
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...
1
2696
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
1
1905
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1585
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.