Why does:
char *myString = (char *) malloc(WORDLIMI T*sizeof(char)) ;
fscanf(openFile , "%s", myString) != EOF;
work yet
char *myString = (char *) malloc(WORDLIMI T*sizeof(char)) ;
fscanf(openFile , "%s", &myString) != EOF;
doesn't (crashes, doesn't result in a compiler error)? I thought the
prior was incorrect, and the latter correct.
Many thanks.
P.S. Yes, I know I shouldn't be using fscanf like this, there are
buffer overflow problems and that I probably should be shot already,
but that's not the question being asked ;) 6 5512 ha*****@gmail.c om wrote:
Why does:
char *myString = (char *) malloc(WORDLIMI T*sizeof(char)) ;
fscanf(openFile , "%s", myString) != EOF;
work yet
char *myString = (char *) malloc(WORDLIMI T*sizeof(char)) ;
fscanf(openFile , "%s", &myString) != EOF;
doesn't (crashes, doesn't result in a compiler error)? I thought the
prior was incorrect, and the latter correct.
<snip>
You're confusing the situation of arrays as opposed to pointers. See
the FAQ: http://c-faq.com/stdio/scanf1a.html http://c-faq.com/aryptr/aryvsadr.html
Taking the address of a pointer yeilds a pointer to _that_ pointer,
not what that pointer points to.
--
Peter ha*****@gmail.c om wrote:
Why does:
char *myString = (char *) malloc(WORDLIMI T*sizeof(char)) ;
In C, you don't need to cast the return value of malloc. Also take the
size of the object using sizeof, rather than the size of the object's
type.
fscanf(openFile , "%s", myString) != EOF;
work yet
char *myString = (char *) malloc(WORDLIMI T*sizeof(char)) ;
fscanf(openFile , "%s", &myString) != EOF;
doesn't (crashes, doesn't result in a compiler error)? I thought the
prior was incorrect, and the latter correct.
fscanf expects a pointer to the object meant to hold the result of each
conversion. In the second snippet, you've passed it a value of type
pointer to pointer to char.
Okay, I understand now. Many thanks :) ha*****@gmail.c om wrote:
<snip>
char *myString = (char *) malloc(WORDLIMI T*sizeof(char)) ;
the cast is unnecessary, and may hide an error. sizeof(char) is 1 by
definition
hence some would omit it.
char *myString = malloc (WORDLIMIT);
<snip>
--
Nick Keighley ha*****@gmail.c om said:
P.S. Yes, I know I shouldn't be using fscanf like this, there are
buffer overflow problems and that I probably should be shot already,
but that's not the question being asked ;)
In my experience, it's best to fix the stuff you know how to fix before
worrying about the stuff you don't know how to fix. That way, you don't get
distracted.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at the above domain, - www.
<ha*****@gmail. comwrote in message
news:11******** **************@ 51g2000cwl.goog legroups.com...
Why does:
char *myString = (char *) malloc(WORDLIMI T*sizeof(char)) ;
fscanf(openFile , "%s", myString) != EOF;
work yet
char *myString = (char *) malloc(WORDLIMI T*sizeof(char)) ;
fscanf(openFile , "%s", &myString) != EOF;
doesn't (crashes, doesn't result in a compiler error)? I thought the
prior was incorrect, and the latter correct.
Many thanks.
P.S. Yes, I know I shouldn't be using fscanf like this, there are
buffer overflow problems and that I probably should be shot already,
but that's not the question being asked ;)
Problem is "pointer to pointer to char" rather than "pointer to char", as
I'm sure you discovered.
One final note: "pointer to pointer to thingie" has its place. Sometimes
when you are using data structures where either:
a)The location might change in memory, OR
b)The canonical form of the empty set is a NULL pointer,
it is necessary to pass a pointer to a pointer to a data structure to a
function. That way, the function can swap out the pointer pointed to if
necessary.
But, as I'm sure you discovered ... not with scanf.
--
David T. Ashley (dt*@e3ft.com) http://www.e3ft.com (Consulting Home Page) http://www.dtashley.com (Personal Home Page) http://gpl.e3ft.com (GPL Publications and Projects) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: John Phung |
last post by:
Is there a fscanf equivalent for c++? Here's what I'm talking about:
unsigned int NextAddress(ifstream& AddressFile) {
unsigned int next_address;
-->How do I rewrite the fscanf listed below using ifstream?
-->fscanf(AddressFile, "%x", next_address);
if(AddressFile.eof()) {
return 0;
|
by: Mantorok Redgormor |
last post by:
I have failed to find an authoritive answer on the following:
fscanf(NULL, ..);
The rest of the function arguments have been omitted.
I'm only curious as to what behavior is given when
the first argument is NULL?
I went through J.2 of the standard but it isn't listed as
|
by: Kay |
last post by:
1) If i want to read data from a txt file,
eg John; 23; a
Mary; 16; i
How can I read the above data stopping reading b4 each semi-colon and
save it in three different variables ?
2) If I enter a number, can I use to call a particular node ?
eg enter a number: 3
calling node of number 3
is it possible ?
|
by: Cal Lidderdale |
last post by:
My input line is i1,i2,i3,i4,i5,i6,i7,i8^,...i596,597, ...
14101,14102...NL/CR
very long line of data - I only want the first 8 items and
the delimiter between 8 & 9 is a carrot "^".
The line can end at the 100th item or the 40,000th item.
My code is:
char data, mynull;
|
by: learner |
last post by:
I have datafiles like this:
0 1941 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.02 0.00 0.00 1
0 1941 0.00 0.03 0.00 0.03 0.04 0.02 0.00 0.00 0.00 0.00 2
0 1941 0.00 0.00 0.00 0.00 0.52 0.00 0.00 0.17 1.07 0.09 3
0 1941 0.04 0.00 0.00 0.00 0.00 0.62 0.00 0.01 0.00 0.00 4
0 1941 0.00 0.02 0.00 0.00 0.00 0.22 0.00 0.00 0.00 0.16 5
0 1941 0.00 0.00 0.00 0.00 0.09 0.04 0.00 0.00 0.00 0.00 6
0 1941 0.00...
| |
by: siliconwafer |
last post by:
Hi All,
here is one code:
int main() {
FILE*fp;
unsigned long a;
fp = fopen("my_file.txt","w+");
a = 24;
fprintf(fp,"%ld",a);
while(fscanf(fp,"%ld",&a) == 1) {
|
by: gb |
last post by:
Hi,
I have a small program which opens a file (Whose content is a mac
address in the form xx:xx:xx:xx:xx:xx) and uses fscanf() to get
individual bytes into an array which is defined as "unsigned char
macaddress". The strange thing is that it prints zeroes on MIPS
processor where as it works correctly on x86 systems. Appreciate if
some body could help to understand the fscanf() behavior.
Here is the code I am using:
|
by: quyvle |
last post by:
I can't seem to get this function to work correctly. I'm wondering
if anyone could help me out with this.
So I'm using the fscanf function to read the input stream and store
each string in the appropriate variables.
Here's what I'm reading from another file:
"# Number of power catergories: 9"
|
by: BlueDream |
last post by:
What I'm trying to do is simply scan in a file then whenever I get a space, turn it into 2 spaces. Whenever I get a carriage return, turn it into 2 spaces..
An example of what I'm trying to achieve
with this body
of text.
An example of what I'm trying to achieve
with this body
|
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,...
|
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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |