Initializing array of pointers to char from a 2d char array - c

I have a function
int executeCommand(char args[][TOKENSIZE]){
char *argv[] = {args[0], args[1]}; // This works
char *argv[] = {args}; // This doesn't
}
The args variable will not be of fixed length so I can't use the first line as it is. I don't want to use malloc or free due to some reasons. Is there any way of initializing this argv array from args without hard coding the number of entries.

The function needs to know the size somehow or it is mission impossible. One possible way is to use pointers to VLA:
int executeCommand(size_t n, char args[n][TOKENSIZE]){
char* argv[n];
for(size_t i=0; i<n; i++)
{
argv[i] = args[i];
}
}
As for you why you can't simply use args instead of copying the array addresses, I have no idea.
A complete hard-copy of the data would look like
char argv[n][TOKENSIZE];
memcpy(argv, args, sizeof(char[n][TOKENSIZE]));

Related

AddressSanitizer error in leetcode practice using c

char * convert(char * s, int numRows){
char rows[numRows][strlen(s)];
memset( rows, '\0', numRows*strlen(s)*sizeof(char) );
int curRow=0;
bool goingDown=false;
int len=0;
char *str=s;
for(char c=*str;c=*str;++str){
len=0;
while(rows[curRow][len]){len++;}
rows[curRow][len]=c;
if(curRow==numRows-1||curRow==0){goingDown=!goingDown;}
if(goingDown){curRow++;}
else{curRow--;}
}
char *zig=malloc(strlen(s)+1);
*zig='\0';
int i=0;
for(char *row;i<numRows;i++){
row=*(rows+i);
zig=strcat(zig,row);
}
return zig;
}`
I am trying to implement the solution in c. Leetcode has thrown this to me:
==26==ERROR: AddressSanitizer: dynamic-stack-buffer-overflow on address 0x7ffd37cc1741.
But I have no idea what is the problem. All I could guess is that row variable has something wrong. Test case being "A" 1 .
row: 0x7ffd37cc1740 "A\vI\037\244U"
So how could I fix this code? Trying to learn more about c.
Problem solved now. I shall declare 2d char rows with strlen(s)+1. Thus the row variable would be nul terminated, which would be valid for strcat argument.Thanks!
Your problem (or at least, one of) is in the first line of the function definition.
char rows[numRows][strlen(s)];
declares a 2D char array on the stack. You should not declare variables with size dependent on runtime in the stack (in this case, because of the variables s and numrows passed to the function).
You should instead allocate memory dynamically using malloc or alternatives -
char ** rows = malloc(sizeof(char *)*numrows); // allocate memory to store `num_rows` char pointers
// for each element in rows, create a pointer with num_columns of chars amount of space
for(int i = 0 ; i < num_columns ; i++){
rows[i] = malloc(sizeof(char) * num_columns); // store pointer in rows[i]
// memset this memory as needed
}
// now rows[x][y] will be one char
Note that you will need to allocate one more char than the number of characters in your string to add the null terminator in C char arrays.
This will likely get rid of the buffer overflow error, although your program may contain other flaws.

warning: assignment makes integer from pointer without a cast, sorting arguments

I know this was asked a few times, but I still do not fully understand the issue.
I have an assigment where I need to save and sort the arguments.
int main(int argc, char* argv[])
{
int c;
char* s = malloc(argc * sizeof(char));
extern char *optarg;
extern int optind;
extern int optopt;
while ( (c = getopt(argc, argv, ":adh")) != -1) {
s[argc] = argv;
switch (c) {
case 'a': printf("a\n");
break;
case 'd': printf("d\n");
break;
case 'h': printf("h\n");
break;
}
}
return 0;
}
I know that it has something to do with me saving a pointer to an integer or not doing it.
char* argv[] is what? An array of pointers?
And s is only an array of chars?
char* argv[] is what? An array of pointers?
When such a declaration appears in a function prototype, as in your code, it is equivalent to char **argv. That's a pointer to a pointer to char. In this particular case, the pointed-to pointer is the first element of an array of pointers.
And s is only an array of
chars?
As you have declared an initialized it, s is a pointer to char, and in particular, it points to the first char in a dynamically allocated block. You can use the block via s as if it were an array of char, but it is not technically an array, and s definitely is not an array. Although related, pointers and arrays are very different things.
The compiler should have told you at least on which line the error occurs, but you did not pass that information on to us. Nevertheless, I think I can guess. This line ...
s[argc] = argv;
... is nonsense. Since s is a pointer to char, s[argc] designates one char (and in C, char is among the integer data types). On the other hand, argv is a pointer (to pointer to char). Although C permits pointers to be converted to integers, it is rarely useful to do so, and anyway, a conforming program must use a cast to perform such a conversion. Since you don't actually use s for anything, your best bet is probably to just remove that line.
For what it's worth, if you wanted to set s to point to one of the command line arguments, say the second, the syntax would be
s = argv[2];
(The zeroth element of argv is conventionally the name of the program; the arguments start at index 1.) But if you intend to do that then you don't need to malloc() any memory for s; whatever you do allocate for it is leaked when you assign a new value to it.
char *argv[] is a pointer to an array of strings. AKA char **argv
int argc is an integer count of the number of strings on the command line, including exe name.
Note also that the length of each string in argv[] has nothing to do with the value of argc. The strings can be as short as two characters, such as a\0 or as long as (or longer than) 1000 characters.
So the statement char* s = malloc(argc * sizeof(char)); will not do what you are thinking it will do.
First it is the form of an expression that will create some space for only one string, not argc of them.
Second, if your command line included 3 item, ( argc == 3 ) and one of the arguments was a string of length 9, say arguments, then your malloc statement would create space for 1 string, with only 3 characters. Not big enough to contain the string `arguments\0', not to mention the other two.
Use something like the following to determine the length of the longest of the argc strings:
int len = 0, lenKeep = 0;
for(i=0;i<argc;i++)
{
len = strlen(argv[i] > len)
if(len > lenKeep) lenKeep = len;
}
Now, lenKeep + 1 has been established as the length needed to contain the longest string (+ 1 for the NULL character, which strlen(...) does not include in its count.). This can be used, along with knowing the number of strings, argc, to allocate memory for each of them.
For example:
int main(int argc, char* argv[])
{
...
char **string = Create2DStr(argc, lenKeep + 1);//will create space
//sufficient to contain
//strings of your command line
...
}
Where Create2DStr(...) could be defined as:
char ** Create2DStr(ssize_t numStrings, ssize_t maxStrLen)
{
int i;
char **a = {0};
a = calloc(numStrings, sizeof(char *));
for(i=0;i<numStrings; i++)
{
a[i] = calloc(maxStrLen + 1, 1);
}
return a;
}

Allocating a char pointer array

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;

Sorting an array of strings in C

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.

Declaring an array of character pointers (arg passing)

This is something that should be easy to answer, but is more difficult for me to find a particular right answer on Google or in K&R. I could totally be overlooking this, too, and if so please set me straight!
The pertinent code is below:
int main(){
char tokens[100][100];
char *str = "This is my string";
tokenize(str, tokens);
for(int i = 0; i < 100; i++){
printf("%s is a token\n", tokens[i]);
}
}
void tokenize(char *str, char tokens[][]){
int i,j; //and other such declarations
//do stuff with string and tokens, putting
//chars into the token array like so:
tokens[i][j] = <A CHAR>
}
So I realize that I can't have char tokens[][] in my tokenize function, but if I put in char **tokens instead, I get a compiler warning. Also, when I try to put a char into my char array with tokens[i][j] = <A CHAR>, I segfault.
Where am I going wrong? (And in how many ways... and how can I fix it?)
Thanks so much!
You would need to specify the size of the second dimension of the array:
#define SIZE 100
void tokenize(char *str, char tokens[][SIZE]);
This way, the compiler knows that when you say tokens[2][5] that it needs to do something like:
Find the address of tokens
Move 2 * SIZE bytes past the start
Move 5 more bytes past that address
???
Profit!
As it stands, without the second dimension specified, if you said tokens[2][5] how would it know where to go?
You're close. Arrays and pointers aren't the same thing, even though it sometimes seems like they are. You can either make your two-dimensional array out of pointers:
char **tokens = malloc(100 * sizeof(char *));
for (i = 0; i < 100; i++)
tokens[i] = malloc(100);
And then use:
void tokenize(char *str, char **tokens)
or you can specify the size of the array in your tokenize() function:
void tokenize(char *str, char tokens[][100])

Resources