473,545 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem in a very simple code

Hi all!
this is the code:
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void MyFunction(char *B[])
{
cout<<sizeof(B) <<endl; //(1)
}

int main()
{
char* B[]={"1","2","we"} ;
MyFunction(B);
cout<<sizeof(B) <<endl;//(2)

system("PAUSE") ;
return 0;
}

and this is the problem :
in the (1) cout I get the answer :4
and in the (2) cout I get 12!
How can I handle this please?


Dec 24 '05 #1
7 1978
Aris wrote:
Hi all!
this is the code:
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void MyFunction(char *B[])
{
cout<<sizeof(B) <<endl; //(1)
}

int main()
{
char* B[]={"1","2","we"} ;
MyFunction(B);
cout<<sizeof(B) <<endl;//(2)

system("PAUSE") ;
return 0;
}

and this is the problem :
in the (1) cout I get the answer :4
and in the (2) cout I get 12!
How can I handle this please?


This should be in the FAQ - but here is a previous thread on the topic.

http://groups.google.com/group/comp....bfe04d1eaf64a8

Options are:

a) pass a length
b) use a template
c) do without
Example of a)
void MyFunction(char *B[], int length )
{
cout<<sizeof(*B )*length<<endl; //(1)
}

Example of b)

template <int N>
void MyFunction(char *(&B)[N])
{
cout<<sizeof(B) <<endl; //(1)
}
Dec 24 '05 #2

Aris wrote in message ...
Hi all!
this is the code:
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void MyFunction(char *B[]){
cout<<sizeof(B )<<endl; //(1)
}

int main(){
char* B[]={"1","2","we"} ;
MyFunction(B );
cout<<sizeof(B )<<endl;//(2)

system("PAUSE" );
return 0;
}

and this is the problem :
in the (1) cout I get the answer :4
and in the (2) cout I get 12!
How can I handle this please?


By using C++?

#include <iostream>
#include <ostream> // for endl.
// #include <cstdlib> // #include <cstdio>
#include <string>
#include <vector>

void MyFunction( std::vector<std ::string> const &Bstr ){
std::cout<<"in "<<__FUNCTION__ <<" size="<<Bstr.si ze()
<<std::endl; //(1)
for(size_t i(0); i< Bstr.size(); ++i){
std::cout<<"Bst r.at("<<i<<")"< <Bstr.at(i)<<st d::endl;
}
}

int main(){
std::string B[]={"1","2","we"} ;
std::vector<std ::string> vBstr;
std::copy(B, B+(sizeof B/sizeof *B), std::back_inser ter(vBstr));
MyFunction( vBstr );
std::cout<<"str ing array size = "<<sizeof B/sizeof *B<<endl;//(2)

system("PAUSE") ;
return 0;
}

--
Bob R
POVrookie
Dec 24 '05 #3
BobR wrote:
Aris wrote in message ...
[redacted]

By using C++?

[redacted]
void MyFunction( std::vector<std ::string> const &Bstr ){
std::cout<<"in "<<__FUNCTION__ <<" size="<<Bstr.si ze()
<<std::endl; //(1)
If you're going to be pedantic to the OP, then try using Standard C++
yourself. __FUNCTION__ is a g++-ism, and is not part of the Standard.
system("PAUSE") ;
return 0;
}

--
Bob R
POVrookie

Dec 25 '05 #4

red floyd wrote in message ...
BobR wrote:

void MyFunction( std::vector<std ::string> const &Bstr ){
std::cout<<"in "<<__FUNCTION__ <<" size="<<Bstr.si ze()
<<std::endl; //(1)


If you're going to be pedantic to the OP, then try using Standard C++
yourself. __FUNCTION__ is a g++-ism, and is not part of the Standard.


Yup, I done screwed up big-time. You are right. I am so very sorry. Can you
ever forgive me? Please?

Dang, you mean there are people out there who use something other than GCC?
<G>
[ "__FUNCTION __" and "__PRETTY_FUNCT ION__" sure are handy. ]

Merry Christmas and a Happy New Year to all!

--
Bob R
POVrookie
Dec 25 '05 #5
BobR wrote:
red floyd wrote in message ... Yup, I done screwed up big-time. You are right. I am so very sorry. Can you
ever forgive me? Please?

Dang, you mean there are people out there who use something other than GCC?
<G>
[ "__FUNCTION __" and "__PRETTY_FUNCT ION__" sure are handy. ]

Merry Christmas and a Happy New Year to all!

[Entire post taken in the spirit it was intended]

No problem Bob. Happy Chanukah!
Dec 25 '05 #6
Hi all!
I want to make a function which will create file names as :
I will load with an integer
from 1 till 5 and receive a responce like this
"file1.txt","fi le2.txt"....."f ile5.txt"
But instead I get crazy things like theese:
file1.txt
file1.txt2
file1.txt23
file1.txt233@
can you help me?

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

char * Create_File_Nam e(int counter_files)
{
char *ch1="file";
char *ch2=".txt";
char string[2];
itoa(counter_fi les,string,10);
char *ch3=strcat(ch1 ,string);
char *fname=strcat(c h3,".txt");
return fname;
}
int main()
{

char* fname;
for(int i=1; i<5; i++)
{
fname=Create_Fi le_Name(i);
cout<<fname<<en dl;
}
int p;
cin>>p;
return 0;
}


Dec 27 '05 #7
There's unfortunately a great deal going wrong here. Your program
invokes undefined behavior, because the buffers you pass to strcat do
not have sufficient size to hold the concatenated strings. If you're
going to use the old C libraries, you should at least read up on them
to avoid such mistakes:
http://www.cplusplus.com/ref/cstring/strcat.html

That said, it's a bad idea to be using all this legacy, C-style stuff
in any case. Unless you are under some absurd constraint to do so, you
should instead use the STL's string, and associated practices. Here is
an updated, working version of your code:

#include <iostream> // for std::cout
#include <string> // for std::string
#include <sstream> // for std::stringstre am

std::string createFileName( int fileIndex) {
std::stringstre am stream;
stream << "file" << fileIndex << ".txt";
stream.flush();
return stream.str();
}

int main(void) {
for(int i=1; i<=5; i++) {
std::cout << createFileName( i) << "\n";
}
return 0;
}

Luke

Dec 28 '05 #8

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

Similar topics

5
569
by: fripper | last post by:
I posted this problem a couple of days ago but felt I might have better luck re-stating the problem. Apparently I messed up IIS (v. 5) somehow because I am suddenly unable to load web forms! A simple example will help. I created a simple web project that contains two simple forms ... WebForm1 and WebForm2. WebForm1 has a button which...
10
2113
by: Saso Zagoranski | last post by:
hi, this is not actually a C# problem but since this is the only newsgroup I follow I decided to post my question here (please tell me where to post this next time if you think this post shouldn't be here). I have two design questions: 1. what is the correct (or best) way to include database queries into the code if you plan on
0
1872
by: 42 | last post by:
I implemented a simple class inherited from Page to create a page template. It simply wraps some trivial html around the inherited page, and puts the inherited page into a form. The problem I have run into is that the emitted html at the end of the process is slightly different and doesn't work. Please don't be put off by all the source...
6
3788
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length is more that 1249 bytes, then the progress bar of the browser will move too slow and then displaying that the page not found!!!! If the message is...
7
5209
by: Ankit Aneja | last post by:
I put the code for url rewrite in my Application_BeginRequest on global.ascx some .aspx pages are in root ,some in folder named admin and some in folder named user aspx pages which are in user folder are using this code of url rewrite project is running completely fine on localhost but after uploading first page...
8
9707
by: Sarah | last post by:
I need to access some data on a server. I can access it directly using UNC (i.e. \\ComputerName\ShareName\Path\FileName) or using a mapped network drive resource (S:\Path\FileName). Here is my problem: my vb.net program has problems with UNC. If the UNC server is restarted or goes off-line, my VB.net program crashes. The code for UNC...
6
2332
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any solution. My code can be downloaded from here: http://www.tprimke.net/konto/PyObject-problem.tar.bz2. There are some scripts for GNU/Linux system...
7
4339
by: Lorenzino | last post by:
Hi, I have a problem with bindings in a formview. I have a formview; in the insert template i've created a wizard control and inside it i have an HTML table with some textboxes bound to the sqldatasource of the formview. If i put this textboxes outside the table everything works well, but as soon as i put them inside the table (in order to...
3
1476
by: Robert Johnson | last post by:
Hi all. Created a simple table in my db. 3 colums one is a Int set for autoincrement. Itentity True, seed 1, Incremement 1, null False. The other colums are simple VarChar(50) null false on the first and true on the last. Nothihng complicated here. create a simple test app in VS2005 and connect to MSSQL2005 server, no problem there. I can...
30
2768
by: Einstein30000 | last post by:
Hi, in one of my php-scripts is the following query (with an already open db-connection): $q = "INSERT INTO main (name, img, descr, from, size, format, cat, host, link, date) VALUES ('$name', '$img', '$descr', '$user', '$size', '$format', '$cat', '$host', '$link', '$date')" or die(mysql_error()); And when the query gets executed i get...
0
7478
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...
0
7410
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...
0
7668
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
5984
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...
1
5343
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...
0
4960
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...
0
3466
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...
1
1025
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
722
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...

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.