473,397 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Clearing a char pointer

I have the following code
Expand|Select|Wrap|Line Numbers
  1. int parseFile(FILE* fp, char s[]) {
  2.     char *buffer;
  3.     int c, i;
  4.  
  5.     i = 0;
  6.     buffer = (char*)malloc(MAX_ARRAY+1);
  7.     while ((c = getc(fp)) != EOF) {
  8.         while (!isspace(c)) {
  9.             *(buffer+i) = c;
  10.             i++;
  11.             c = getc(fp);
  12.         }
  13.         i = 0;
  14.         if (strcmp(buffer, s) == 0) {
  15.             return 0; /* found */
  16.         }
  17.     }
  18.     return 1; /* not found */
  19. }
  20.  
and the problem is when I run it, each time through the loop it will still retain what was left over if the previous loop had more chars. I want to blank out this pointer each time through the loop so I will get clean results. I thought using free() would help, but it did nothing.
Nov 17 '08 #1
2 8072
arnaudk
424 256MB
You have to initialize your buffer. You allocated memory for it alright but that memory can contain any junk, including what happened to be there before. free() releases the memory but doesn't wipe the buffer either.

You can initialize your buffer, setting all bits to zero, by using calloc() instead of malloc(). Either use that together with free() to reallocate a clean buffer each time, or just overwrite the buffer's contents with zeros.
Nov 17 '08 #2
vmpstr
63
In C, a string is a collection of characters that ends with 0 (or '\0' if you prefer).

So when reading character by character, and filling in the buffer, you want to ensure that the next character is '\0'.

something along the lines.

*(buffer + i) = c;
*(buffer + i + 1) = 0;

and conveniently, this is sort of equivalent to clearing it all out with 0s.
Nov 18 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Hans B | last post by:
Please bear with me if I ask silly questions....I am a somewhat newbie to the C language.... If I had: struct test1_{ unsigned char mycount1; unsigned short mycount2; unsigned short...
4
by: Roubles | last post by:
Hi All, Quick question; what is the difference between initializing and clearing a structure like this: mystruct_t a = {0}; and initializing and clearing it like this:
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
29
by: Daniel Rudy | last post by:
Hello, Consider the following code fragment: fileptr = fopen(param->filename, "r"); if (fileptr == NULL) { error("digest.c: digest_file: Open File ", errno); return(-2); }
5
by: max | last post by:
Dear all, I did the following analysis to conclude that the following pointer types are not compatible. Please let me know If my analysis and interpretation of the C standard are correct: ...
3
by: ZMan | last post by:
The following code won't compile with gcc version 3.4.2 (mingw-special). How come? Error: cannot convert `char (*)' to `char**' /**********************************************************/...
9
by: Peithon | last post by:
Hi, This is a very simple question but I couldn't find it in your FAQ. I'm using VC++ and compiling a C program, using the /TC flag. I've got a function for comparing two strings int...
3
by: Martin | last post by:
Is clearing a structure the following way well defined in C89? The structure ACTION contains no floating point or pointer members. Only integral types. My thoughts concern the padding - can and...
20
by: sirsnorklingtayo | last post by:
hi guys please help about Linked List, I'm having trouble freeing the allocated memory of a single linked list node with a dynamic char* fields, it doesn't freed up if I use the FREE()...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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...
0
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...
0
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,...

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.