473,474 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Create 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 1967
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.size()
<<std::endl; //(1)
for(size_t i(0); i< Bstr.size(); ++i){
std::cout<<"Bstr.at("<<i<<")"<<Bstr.at(i)<<std::en dl;
}
}

int main(){
std::string B[]={"1","2","we"};
std::vector<std::string> vBstr;
std::copy(B, B+(sizeof B/sizeof *B), std::back_inserter(vBstr));
MyFunction( vBstr );
std::cout<<"string 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.size()
<<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.size()
<<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_FUNCTION__" 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_FUNCTION__" 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","file2.txt"....."file5.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_Name(int counter_files)
{
char *ch1="file";
char *ch2=".txt";
char string[2];
itoa(counter_files,string,10);
char *ch3=strcat(ch1,string);
char *fname=strcat(ch3,".txt");
return fname;
}
int main()
{

char* fname;
for(int i=1; i<5; i++)
{
fname=Create_File_Name(i);
cout<<fname<<endl;
}
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::stringstream

std::string createFileName(int fileIndex) {
std::stringstream 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
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...
10
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...
0
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...
6
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...
7
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...
8
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...
6
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...
7
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...
3
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...
30
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',...
0
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,...
0
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...
0
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,...
0
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...
1
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...
1
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.