h2xs -cn MyModule
A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.
Jump to
Showing posts with label c. Show all posts
Showing posts with label c. Show all posts
Freeing memory in C
If you allocate memory in a subroutine for a variable that you then return, you’ve got to free the variable you return:
char * allocstring(char * text)
{
char * s = malloc(s, strlen(text)+1);
strcpy(s, text);
return s;
}
void main()
{
char * s = allocstring(“Hello World”);
free(s);
}
Labels:
c,
code,
malloc,
memory,
programming
Regular expressions in C: Advice
If you can avoid using regular expressions in C, then *do not use them!!*
Instead try the strstr (find substring) and strtok (like perl's split) routines.
Labels:
c,
idea,
programming,
regex,
strings
These warnings:
warning: initialization makes pointer from integer without a cast
warning: assignment makes pointer from integer without a cast
...for a line containing malloc could mean that you forgot to #include "stdlib.h".
Functions are assumed to return an integer type by default if not declared.
C printf and scanf format specifiers
Format Specifier | Type |
|---|---|
%d (or %i) | int |
%c | char |
%f | float |
%lf | double |
%s | string |
| %x | hexadecimal |
Comparing strings in C
In c it is not possible to directly compare two strings so a statement like if (string1==string2) is not valid. Most c libraries contain a function called the strcmp().This is used to compare two strings in the following manner.
if(strcmp(name1,name2)==0)
puts("The names are the same");
else
puts("The names are not the same.");
Thanks johnt
if(strcmp(name1,name2)==0)
puts("The names are the same");
else
puts("The names are not the same.");
Thanks johnt
Labels:
c,
code,
programming,
strings
debugging technique
When programming C, it can be difficult to track down a segmentation fault.
It is often caused by attempting to read uninitialised memory.
Try skipping parts of the program until you locate the statement causing it.
Then replace each part of the data by hard-coded values, one by one, until you find the uninitialised variable/pointer.
C pointer error
This error:
...could mean you need to use structure->member instead of structure.member, as sturcture is a pointer.
c function errors
Errors like these:
`function' was previously implicitly declared to return `int'
...point to a missing function declaration at the top of the file.
Labels:
c,
error,
function,
programming
Concatenate C strings, and return them from a function
Don't do this. Just use strcat() and strncpy() instead.#include <stdlib.h>
char* concat(char*, char*);
int main (int argc, char** argv)
{
char *x = "something";
char *y = " completely different.";
char *combined = concat(x, y);
printf("And now for %s\n", combined);
}
char* concat(char *a, char *b)
{
char *target = malloc( strlen(a) + strlen(b) + 1 );
strcpy(target,a);
strcat(target,b);
return target;
}
Labels:
c,
code,
programming
C static
The keyword 'static' before a function name means that the function will only be available to callers in the file where it is declared.
Labels:
c,
programming
Regular expressions in C
Using glibc's regex.h
- Make sure the number of expected matches (nmatch) is high enough, or there will be garbage at the end of the matchptr array
- If regexec() succeeds [returns zero], matchptr contains:
- 0: the whole regex
- 1: first parenthesis match
- if regexec() fails [returns a value], don't even look in matchptr
- Characters that must be escaped:
- parentheses
- plus signs
- If you're matching the end of line, it must be inside any parenthesis!
Labels:
c,
programming,
regex
Subscribe to:
Posts (Atom)