Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
[COLOR=green]/* These illustrate the concept */[/color]
char c = 'x';
char *const p = &c;
char *const * q = &p;
[COLOR=green]/* These are extras */[/color]
char c2 = '5';
char *const p2 = &c2;
[COLOR=green]/* These are ok */[/color]
**q = 'z'; [COLOR=green]/* Modifying the character */[/color]
q = &p2; [COLOR=green]/* Modifying q to point elsewhere*/[/color]
[COLOR=red]/* This isn't ok */[/color]
*q = &c2; [COLOR=red]/* ERROR: Modifying the pointed-to ([b]const[/b]) pointer */[/color]
[COLOR=green]/* Given this: */[/color]
void * random_name = something();
char c3 = 'M';
char *const p3 = *(char* const*)random_name;
[COLOR=green]/* You can do this */[/color]
*p3 = '@'; /* Modify the pointed-to character */
[COLOR=red]/* But not this */[/color]
p3 = &c3; [COLOR=red]/* ERROR: Modifying a const pointer */[/color]
char * const *
| | | |
| | | +- pointer to
| | +------- constant
| +--------- pointer to
+-------------- char
char * const * const
| | | | |
| | | | +- constant
| | | +--- pointer to
| | +--------- constant
| +----------- pointer to
+---------------- char
const char * const * const
| | | | | |
| | | | | +- constant
| | | | +--- pointer to
| | | +--------- constant
| | +----------- pointer to
| +---------------- [s]char[/s]
+---------------------- constant char
char const * const * const
| | | | | |
| | | | | +- constant
| | | | +--- pointer to
| | | +--------- constant
| | +----------- pointer to
| +----------------- constant
+---------------------- char
char *line;
line=malloc(some_space)
return line
char **lines;
while ((line = f())
lines[i]=line
// of course there are a lot of allocs and reallocs to provide proper memory allocations but they are not as important
int CompareStrings(const void *left, const void *right)
{
return strcmp(*(char * const *)left, *(char * const *)right);
}
return strcmp( (const char*)left, (const char*)right );
#include <string.h>
#include <stdio.h>
int CompareStrings(const void *left, const void *right)
{
return strcmp( (const char*)left, (const char*)right );
/* This return crashes...
return strcmp(*(char * const *)left, *(char * const *)right);
*/
}
int main()
{
int ret;
const char str1[] = "Hello";
const char str2[] = "hello";
ret = CompareStrings( str1, str2 );
printf( "ret = %d", ret );
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
char* getline(FILE* infile)
{
char *line = NULL, *tmpline, ch;
int currsize = 1, currpos = 0;
while ((ch = fgetc(infile)) != EOF && ch != '\n')
{
/* if buffer is too small */
if (currpos >= currsize - 1)
{
currsize *= 2;
tmpline = realloc(line, currsize);
if (tmpline)
line = tmpline;
else
{
perror("Memory error");
free(line);
return NULL;
}
}
line[currpos++] = ch;
}
if (currpos)
{
line[currpos] = '\0';
return line;
}
else if (ch == '\n')
return strdup("");
else
return NULL;
}
/* !!!! HERE'S THE PROBLEM !!!! */
/* ***************************************** */
int CompareStrings(const void *left, const void *right)
{
return strcmp(*(char * const *)left, *(char * const *)right);
} /*this version works but it is veeery strange*/
/* ***************************************** */
/* ***************************************** */
int compName(const void *left, const void *right){
return strcmp(*((char **)left), *((char **)right));
} /*this version of compName works as well I've created it myself but I guess it's wrong*/
/* ***************************************** */
/* ***************************************** */
int compName{
return strcmp( (const char*)left, (const char*)right );
}
/* this version doesn't work. Compile with no errors but it doesn't sort array properely*/
/* ***************************************** */
int main(int argc, char *argv[])
{
char **lines=NULL, **tmplines=NULL, *line=NULL;
int numberOfLines=0, i, size;
FILE * infile, *outfile;
int operation;
if (argc!=3){
printf ("Argument error \n");
return 1;
}
infile=fopen(argv[1],"r");
if (!infile){
perror("File error");
exit(1);
}
outfile=fopen(argv[2],"w");
if (!infile){
perror("File error");
exit(1);
}
while((line=getline(infile))!=NULL){
tmplines=realloc(lines,(++numberOfLines)*sizeof(char *));
if(tmplines)
lines=tmplines;
else{
perror("Memory error");
for(i=0;i<numberOfLines-1;i++)
free(lines[i]);
if (lines)
free (lines);
return 1;
}
lines[numberOfLines-1]=line;
}
qsort(lines,numberOfLines,sizeof(char *),compName);
for (i=0; i<numberOfLines; i++){
operation=fputs(lines[i],outfile);
operation=fputs("\n",outfile);
if (!operation){/*tu bys zrobil byka!!!!*/
perror("Error writing data");
for(i=0;i<numberOfLines-1;i++) /*spisz sobie makroinstrukcje!!!*/
free(lines[i]);
if (lines)
free (lines);
return 1;
}
}
for (i=0; i<numberOfLines; i++)
free(lines[i]);
if (lines)
free(lines);
fclose(infile);
fclose(outfile);
return 0;
}
int compName{
return strcmp( (const char*)left, (const char*)right );
}
int CompareStrings(const void *left, const void *right)
{
return strcmp(*(char * const *)left, *(char * const *)right);
} /*this version works but it is veeery strange*/
I obviously forgot to paste function's arguments list (should have been)That isn't even a function. I'm extremely surprised that it compiles.Code:int compName{ return strcmp( (const char*)left, (const char*)right ); }
int compName(const void *left, const void *right){
return strcmp( (const char*)left, (const char*)right );
}
/* ***************************************** */
int compName(const void *left, const void *right){
return strcmp(*((char **)left), *((char **)right));
} /*this version of compName works as well I've created it myself but I guess it's wrong*/
/* ***************************************** */
Obviously you are right. It's my another missprint, again I am sorry for that.Also, when you open outfile, you should be error-checking outfile, not infile.