I am attempting to read a file with some random names in the format "ADAM","MARK","JESSIE" .....
I have some constraints, the file should be read in a function but should be accessible form the main function with no global variables.
The file size and the no of names in the file are not known.
This is what I have done till now. I have some difficulty with dynamic 2d array as I have not used them much.
/* Function to read from the file */
int read_names(FILE *input, char ***names, int *name_count)
{
int f1,size,count,i,j=0;
char **name_array,*text,pos=0;
/* get the file size */
f1=open("names.txt",O_RDONLY);
size=lseek(f1,0,SEEK_END);
close(f1);
/* Reading all the characters of the file into memory */
//Since file size is known we can use block transfer
text=(char *) malloc(size * sizeof(char) );
fscanf(input,"%s",text);
/* Finding the no of names in the file */
for(i=0;i<size;i++)
{
if(text[i]==',')
count++;
}
printf("No. of names determined\n");
/* Assigning the Name count to the pointer */
name_count=(int*)malloc(sizeof(int));
*name_count=count;
name_array=(char **) malloc(count * sizeof(char *));
for(i=0;i<count;i++)
{
name_array[i]=(char*) malloc(10 *sizeof(char ));
}
for(i=0;i<size;i++)
{
if(text[i]!='"')
if(text[i]==',')
{
**name_array[pos][j]='\0'; //error here
pos++;
j=0;
}
else
name_array[pos][j++]=text[i];
}
printf("Names Counted\n");
printf("Total no of names: %d\n",*name_count);
names=(char ***) malloc(sizeof(char **);
names=&name_array;
return 1;
}
/* Main Function */
int main(int argc, char *argv[])
{
FILE *fp;
char ***names;
int *name_count;
int status;
// Opening the file
fp = fopen("names.txt","r");
// Now read from file
status = read_names(fp,names,name_count);
printf("From Main\n");
fclose(fp);
system("PAUSE");
return 0;
}
I use WxDev and am getting an error "invalid type argument of `unary *' when I try to assign the null character.
Any pointers on how to do this?
Take a closer look at this expression from the line generating the error and think about what it's doing:
**name_array[pos][j]
Remembering that brackets have higher precedence than unary *, this is equivalent to *(*((name_array[pos])[j])), which is 2 subscripts followed by 2 dereferences. That's a total of 4 dereferences, since foo[i] is equivalent to *(foo+i). The declared type of name_array is char **, which means you can only dereference it twice. Think of the type declaration char **name_array as meaning that **name_array has type char, since that's the basis for type declaration syntax (see the History of C, "Embryonic C").
Off topic
Another issue arises on the line:
name_array[i]=(char*) malloc(sizeof(char ));
Here, you're only allocating enough space for a single character in each element of name_array.
**name_array[pos][j]='\0'; \\error here
I see that name_array is declared as
char **name_array
Problem is that
**name_array[pos][j] tries to dereference (twice!!) a character
** (name_array[pos]) [j]; /* name_array[pos] is of type (char*) */
** ( (name_array[pos]) [j] ); /* name_array[pos][j] is of type (char) */
You cannot dereference a character.
Suggestion: simplify your code.
When code doesn't "behave", it usually is not because it is too simple ;)
This might seem pedantic, but it is important. You aren't using a 2D array. In fact, you don't create ANY arrays in your code.
One of the important things to understand is that arrays "decay" into pointers.
link text
Much of the confusion surrounding arrays and pointers in C can be traced to a misunderstanding of this statement. Saying that arrays and pointers are equivalent'' means neither that they are identical nor even interchangeable. What it means is that array and pointer arithmetic is defined such that a pointer can be conveniently used to access an array or to simulate an array. In other words, as Wayne Throop has put it, it'spointer arithmetic and array indexing [that] are equivalent in C, pointers and arrays are different.'')
Related
I have set a structure type:
typedef struct {
char *snt[MAX_LINE_LENGTH];
} sentence;
And this line is getting a cast specifies array type error:
sentence copySentence(sentence *source) {
sentence nw;
nw.snt = (char *[])source->snt; //Here is the error
return nw;
}
What is the best fix for this code line and what is the problem?
Both nw.snt and source->snt are arrays of pointer. To "deep copy" the whole array, you may want to use memmove(nw.snt, source->snt, MAX_LINE_LENGTH * sizeof (char *));.
Also, people usually prefer passing a pointer to a struct than pass that struct directly to reduce the cost of argument passing. In this case, you can
sentence *copySentence(sentence *source) {
sentence *nw;
nw = malloc(sizeof (struct sentence));
memmove(nw.snt, source->snt, MAX_LINE_LENGTH * sizeof (char *));
return nw;
}
You are declaring snt as an array of pointers to charactes. You probably mean it t be an array of charactes, or a pointer to an array of characters:
char snt[MAX_LINE_LENGTH]; // array of characters to hold your sentence
char *snt; // pointer to array of characters
When you assign an element to a compatible element, there is no need for a cast and casting is here considered harmful because you prevent the compiler from giving you warnings. Note that you do not copy the characters, you only make two structs point to the same sentence.
I leave fixing this to you, as an excercise.
Im not clearly sure what is the difference between those 2. My professor wrote that **array is same as *array[] and we were presented an example where he used **array (so after classes I tried exchanging that with *array[] and it didn't work), could anyone tell me if those 2 are actually the same as he wrote?? Anyway the class was about dynamic memory allocation
#As soon as I changed the double pointer, this line started throwing error
lines = malloc(sizeof(char*));
and a few others where the memory is beeing reallocated
#2 Hell yeah, here's the whole code
And for those comments bellow, nope there is nothing inside [] because his statement was
**array = *array[]
BIG UPDATE
I am so sorry for any inconvenience, I was just too tired at the time of writing this post, here is the whole code with no edits
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char **lines; // global text buffer, organized as an array of lines
// --------------------------------------------------------------------------------
// initialize global buffer
void initialize()
{
lines = malloc(sizeof(char*));
lines[0] = NULL;
}
// --------------------------------------------------------------------------------
// return number of lines in buffer
int countLines()
{
int count = 0;
while(lines[count++]) ;
return count-1;
}
// --------------------------------------------------------------------------------
// print one line
void printLine(int line)
{
printf("Line %d: %p %p %s\n",line, &lines[line], lines[line], lines[line]);
}
// --------------------------------------------------------------------------------
// print all lines
void printAll()
{
int num_lines = countLines();
int line = 0;
printf("----- %d line(s) ----\n",num_lines);
while (line < num_lines)
printLine(line++);
printf("---------------------\n");
}
// --------------------------------------------------------------------------------
// free whole buffer
void freeAll()
{
int line = countLines();
while (line >= 0)
free(lines[line--]);
free(lines);
}
// --------------------------------------------------------------------------------
// insert a line before the line specified
void insertLine(int line, char *str)
{
int num_lines = countLines();
// increase lines size by one line pointer:
lines = realloc(lines, (num_lines+2) * sizeof(char*));
// move line pointers backwards:
memmove(&lines[line+1], &lines[line], (num_lines-line+1)*sizeof(char*));
// insert the new line:
lines[line] = malloc(strlen(str)+1);
strcpy(lines[line],str);
}
// --------------------------------------------------------------------------------
// remove the specified line
void removeLine(int line)
{
int num_lines = countLines();
// free the memory used by this line:
free(lines[line]);
// move line pointers forward:
memmove(&lines[line], &lines[line+1], (num_lines-line+1)*sizeof(char*));
// decrease lines size by one line pointer:
lines = realloc(lines, num_lines * sizeof(char*));
}
// --------------------------------------------------------------------------------
// insert a string into specified line at specified column
void insertString(int line, int col, char *str)
{
// make room for the new string:
lines[line] = realloc(lines[line], strlen(lines[line])+strlen(str)+1);
// move characters after col to the end:
memmove(lines[line]+col+strlen(str), lines[line]+col, strlen(lines[line])-col);
// insert string (without terminating 0-byte):
memmove(lines[line]+col, str, strlen(str));
}
// --------------------------------------------------------------------------------
// MAIN program
int main()
{
initialize();
printAll();
insertLine(0,"Das ist");
printAll();
insertLine(1,"Text");
printAll();
insertLine(1,"ein");
printAll();
insertLine(2,"kurzer");
printAll();
printf("lines[2][4] = %c\n",lines[2][4]);
insertString(2,0,"ziemlich ");
printAll();
removeLine(2);
printAll();
freeAll();
return 0;
}
If the code you reference in your question was given to you by your professor as an example of the use of pointer arrays of pointers to pointers, I'm not sure how much good that class will actually do. I suspect it was either provided as a debugging exercise or it may have been your attempt at a solution. Regardless, if you simply compile with Warnings enabled, you will find a number of problems that need attention before you advance to debugging your code.
Regarding the code you reference, while you are free to use a global text buffer, you are far better served by not using a global buffer and passing a pointer to your data as required. There are some instances, various callback functions, etc. that require global data, but as a rule of thumb, those are the exception and not the rule.
Your question basically boils down to "How do I properly use an array of pointers and double-pointers (pointer-to-pointer-to-type) variables. There is no way the topic can be completely covered in one answer because there are far too many situations and contexts where one or the other can be (or should be) used and why. However, a few examples will hopefully help you understand the basic differences.
Starting with the array of pointers to type (e.g. char *array[]). It is generally seen in that form as a function argument. When declared as a variable it is followed with an initialization. e.g.:
char *array[] = { "The quick",
"brown fox",
"jumps over",
"the lazy dog." };
char *array[]; by itself as a variable declaration is invalid due to the missing array size between [..]. When used globally, as in your example, the compiler will accept the declaration, but will warn the declaration is assumed to have one element.
The elements of array declared above are pointers to type char. Specifically, the elements are pointers to the string-literals created by the declaration. Each of the strings can be accessed by the associated pointer in array as array[0], ... array[3].
A pointer to pointer to type (double-pointer), is exactly what its name implies. It is a pointer, that holds a pointer as its value. In basic terms, it is a pointer that points to another pointer. It can be used to access the members of the array above by assigning the address of array like:
char **p = array;
Where p[1] or *(p + 1) points to "brown fox", etc.
Alternatively, a number of pointer to pointer to type can be dynamically allocated and used to create an array of pointers to type, that can then be allocated and reallocated to handle access or storage of an unknown number of elements. For example, a brief example to read an unknown number of lines from stdin, you might see:
#define MAXL 128
#define MAXC 512
...
char **lines = NULL;
char buf[MAXC] = {0};
lines = malloc (MAXL * sizeof *lines);
size_t index = 0;
...
while (fgets (buf, MAXC, stdin)) {
lines[index++] = strdup (buf);
if (index == MAXL)
/* reallocate lines */
}
Above you have lines, a pointer-to-pointer-to-char, initially NULL, that is use to allocate MAXL (128) pointers-to-char. Lines are then read from stdin into buf, after each successful read, memory is allocated to hold the contents of buf and the resulting start address for each block of memory is assigned to each pointer line[index] where index is 0-127, and upon increment of index to 128, index is reallocated to provide additional pointers and the read continues.
What makes the topic larger than can be handled in any one answer is that an array of pointers or pointer to pointer to type can be to any type. (int, struct, or as a member of a struct to different type, or function, etc...) They can be used linked-lists, in the return of directory listings (e.g opendir), or in any additional number of ways. They can be statically initialized, dynamically allocated, passed as function parameters, etc... There are just far too many different contexts to cover them all. But in all instances, they will follow the general rules seen here and in the other answer here and in 1,000's more answers here on StackOverflow.
I'll end with a short example you can use to look at the different basic uses of the array and double-pointer. I have provided additional comments in the source. This just provides a handful of different basic uses and of static declaration and dynamic allocation:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void) {
/* array is a static array of 4 pointers to char, initialized to the
4 string-literals that a part of the declaration */
char *array[] = { "The quick",
"brown fox",
"jumps over",
"the lazy dog." };
/* p is a pointer-to-pointer-to-char assigned the address of array */
char **p = array;
/* lines is a pointer-to-pointer-to-char initialized to NULL, used
below to allocate 8 pointers and storage to hold 2 copes of array */
char **lines = NULL;
size_t narray = sizeof array/sizeof *array;
size_t i;
printf ("\nprinting each string-literal at the address stored by\n"
"each pointer in the array of ponters named 'array':\n\n");
for (i = 0; i < narray; i++)
printf (" %s\n", array[i]);
printf ("\nprinting each string using a pointer to pointer to char 'p':\n\n");
for (i = 0; i < narray; i++, p++)
printf (" %s\n", *p);
p = array;
printf ("\nprinting each line using a pointer to pointer"
" to char 'p' with array notation:\n\n");
for (i = 0; i < narray; i++)
printf (" %s\n", p[i]);
/* allocate 8 pointers to char */
lines = malloc (2 * narray * sizeof *lines);
/* allocate memory and copy 1st 4-strings to lines (long way) */
for (i = 0; i < narray; i++) {
size_t len = strlen (array[i]);
lines[i] = malloc (len * sizeof **lines + 1);
strncpy (lines[i], array[i], len);
lines[i][len] = 0;
}
/* allocate memory and copy 1st 4-strings to lines
(using strdup - short way) */
// for (i = 0; i < narray; i++)
// lines[i] = strdup (array[i]);
/* allocate memory and copy again as last 4-strings in lines */
p = array;
for (i = 0; i < narray; i++, p++)
lines[i+4] = strdup (*p);
p = lines; /* p now points to lines instead of array */
printf ("\nprinting each allocated line in 'lines' using pointer 'p':\n\n");
for (i = 0; i < 2 * narray; i++)
printf (" %s\n", p[i]);
/* free allocated memory */
for (i = 0; i < 2 * narray; i++)
free (lines[i]);
free (lines);
return 0;
}
Let me know if you have any questions. It a large topic with a relatively small set of rules that can be applied in whole lot of different ways and in different contexts.
My professor wrote that **array is same as *array[]
That is true in some contexts and not true in other contexts.
If used in a function as argument,
void foo(int **array) {}
is the same as
void foo(int *array[]) {}
When declared as variables,
int **array;
is not the same as
int *array[];
To Explain in short:
if you want to use a pointer to type, you use *array or *ptr, or whatever variable name .
if you want to use an array of pointers and you already know how many pointers you need before execution (for eg. 10 pointers), then you use *array[10] or *ptr[10];
if you want to use an array of pointers but yet you don't know how many pointers you need before execution, then you use **array or **ptr;
after that you can dynamically allocate memory for every pointer using malloc or new operator to allocate memory for whatever data type you are using, (the data type maybe primitive like int, char ,etc, or it maybe user-defined like struct, etc.
int main()
{
char** subject_array;
char** courses_array;
initialize(subject_array, courses_array);
}
void initialize(char*** subject_array, char*** courses_array)
{
int i;
*subject_array = (char**) malloc(100 * sizeof(char*)); // 100 char pointers
*courses_array = (char**) malloc(100 * sizeof(char*));
for(i = 0; i < 100; i++) //malloc for subject_array
{
(*subject_array)[i] = (char*) malloc(4 * sizeof(char)); // 4 chars for each
(*courses_array)[i] = (char*) malloc(6 * sizeof(char)); // char pointer
}
} //void initialize
My question is: Why is there a discrepancy between the declared double pointers in main and the triple pointers in the initialize function? I think the triple pointer allows me to modify where the 2d array points.
My assignment gives these requirements:
Since you will be changing to where the pointers point, this will involve triple pointers for subjects and
courses!
The above code takes care of bolded requirement but I have absolutely no idea what this even means.
Can someone break it down to me? Does that mean I can modify contents of the 2d arrays of subject_array and courses_array? Such as swapping?
But I can do that already with just double pointers making a 2d array! WHY triple pointers?
First of all, the code is incorret, because initialize asks for char***'s and main gives it char**'s. To add another "pointer level", you take the address of whatever you want to give it to. So, the last line of main shall read initialize(&subject_array, &courses_array) (or shall it be return 0?)
After all, if you don't need to modify the variable subject_array and courses_array themselves, not what they point to, you may remove those pointers. And you actually do modify those variables themselves by means of *var = malloc(...). And, as I already noted, the only problem is that the arguments to initialize are char***, char***, not char**, char** as main is doing.
I've got a number of files that I am reading the contents into a char array, I would like to store the contents I read from each file in an array of characters so that I could access the separate character arrays (file contents) by index, I am not sure of the syntax on how to do this.
My understanding is that I need to set each of the memory addresses in my patterns array to one memory address per char array; to the char array stored in patternData but I do not know how to set this. What is the syntax that I am missing that I need to get this to happen?
What I've tried
I would have thought that If I was storing a type of char* then I would need a type char** to store the separate arrays of char arrays.
I would access a char* by using the following notation to set the memory address of the pattern index to
&patterns[INDEX] = &pData;
However this does not work. There is a plethora of "char pointer array" questions but I'm not sure of the correct way to do this simple assignment of pData to an index of patterns.
char *tData;
int tLength;
char *pData;
int pLength;
char **patterns;
void ReadFromFile(FILE *f, char **data, int *length) //This is what is passed into function
int main(int argc, char **argv)
{
for (int i = 1; i <= 5; i++)
{
FILE *f;
char fileName[1000];
sprintf(fileName, "file%d.txt", i);
f = fopen(fileName, "r");
if (f == NULL)
return 0;
ReadFromFile(f, &pData, &pLength); //works to here, contents of file assigned to pData
fclose(f);
&patterns[i - 1] = &pData;
}
return 0;
}
This line is incorrect:
&patterns[i - 1] = &pData;
You are trying to assign to the result of the "take an address" operator, which is not possible because it's not an lvalue.
The assignment should be as follows:
patterns[i - 1] = pData;
but you need to allocate patterns before you do this. You can use
patterns = malloc(sizeof(char*)*5);
or simply declare patterns to be an array of five:
char *patterns[5];
This assumes that your ReadFromFile function allocates a char*, and assigns it to the address pointed to by pData. Note that you need to free all pointers that were obtained through malloc/calloc/realloc.
I haven't read particularly carefully, but it strikes me as odd that you're taking the reference of patterns when it's a **. This is in the line:
&patterns[i-1] = &pData;
I have an assignment I've been working on for a few hours now, and I can't seem to get it quite right. The assignment is to take a random number of names (from stdin), sort them, and then output them in alphabetical order. I can't find any sites online that handle this kind of sorting specifically, and have had no luck trying to implement qsort() into my code.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int stringcmp(const void *a, const void *b)
{
const char **ia = (const char **)a;
const char **ib = (const char **)b;
return strcmp(*ia, *ib);
}
void main(int argc, char *argv[])
{
char *input[] = {" "};
char temp[20][20];
int i = 0;
int num = 0;
int place = 0;
int stringlen = sizeof(temp) / sizeof(char);
printf("How many names would you like to enter? ");
scanf("%d", &num);
while (place < num)
{
printf("Please input a name(first only): ");
scanf("%s", input[place]);
printf("The name you entered is: ");
printf("%s\n", input[place]);
place++;
}
//qsort(temp, stringlen, sizeof(char *), stringcmp); <-- just an idea I was messing with
qsort(input, stringlen, sizeof(char *), stringcmp);
printf("Names:\n");
for(i=0; i<place; i++)
printf("%s\n", input[i]);
system("PAUSE");
return(EXIT_SUCCESS);
}
The main problem is, when I go to output my code, I cannot use the char *input variable because of how its declared. The temp[] will display, but will not be sorted by qsort because it is not declared as a pointer. Any ideas?
You can't declare your input array like that. Since you know how many the user requires, you can dynamically allocate the array:
char **input = malloc(num * sizeof(char*));
Likewise, when you read your strings in, they need somewhere to go. Simply passing an uninitialized pointer to scanf is not okay. I suggest you define the maximum length of a name and have a temporary buffer for reading it:
const size_t MAX_NAME = 50;
char name[MAX_NAME];
...
for( i = 0; i < num; i++ )
{
printf("Please input a name(first only): ");
scanf("%s", name);
input[i] = strdup(name);
}
[Note this does not prevent the user from overflowing the 'name' buffer. I used scanf for illustrative purposes only]
You seem to be passing the wrong array length to qsort. Try this:
qsort(input, num, sizeof(char *), stringcmp);
When you are finished, you need to release memory for all the names and the array.
for( i = 0; i < num; i++ ) free(input[i]);
free(input);
could you explain
the ** declarations throughout the code? I'm not sure what they're
used for, although I know the function for stringcmp is a widely used
algorithm, I have no idea how it works; I'm thrown off by the double
de-reference markers.
Yep, in the case where I used it, I am telling C that to get a single character, I have to dereference a pointer twice. When you index a pointer, it's dereferencing. So I allocated an array by requesting a block of memory containing num * sizeof(char*) bytes. Because I assigned that pointer to a char**, the compiler knows that I am pointing to a chunk of memory that contains char* values.
If I ask for input[0] (this is the same as *input) it should look at the very start of that memory and pull out enough bytes to form a char*. When I ask for input[1], it skips past those bytes and pulls out the next bunch of bytes that form a char*. Etc... Likewise, when I index a char*, I am pulling out single characters.
In your stringcmp function, you have the following situation. You passed a void* pointer to qsort so it doesn't actually know the size of the data values stored in your array. That's why you have to pass both the array length AND the size of a single element. So qsort just blindly rips through this arbitrary-length array of arbitrary-sized values and fires off memory addresses that ought to contain your data for comparison. Because qsort doesn't know anything else about your array elements except where they are located, it just uses void*.
But YOU know that those pointers are going to be the memory addresses of two of your array elements, and that your array elements are char*. So you need the address of a char* (hence you cast the pointers to char**). Now you need to dereference these pointers when you call strcmp() because that function requires a char* (ie a value that points directly to the memory containing your string characters). That is why you use the * in strcmp(*ia, *ib).
One quick way to fix your program is to declare input as an array of pointers, like this:
char *input[20];
When you read names in, use tmp[place] for your buffer, and store the pointer into input, like this:
scanf("%19s", tmp[place]);
input[place] = tmp[place];
Now sorting the input should work fine.
This has a limitation of being limited to 20 lines of 20 characters max. If you learned about malloc in the class, you should be able to fix that by allocating your strings and the string array dynamically.