character-arrays

using C Pointer with char array

荒凉一梦 提交于 2020-01-13 04:55:07
问题 int i=512; char *c = (char *)&i; c[0] =1; printf("%d",i); this displays "513", it adds 1 to i. int i=512; char *c = (char *)&i; c[1] =1; printf("%d",i); whereas this displays 256. Divides it by 2. Can someone please explain why? thanks a lot 回答1: Binary The 32-bit number 512 expressed in binary, is just: 00000000000000000000001000000000 because 2 to the power of 9 is 512. Conventionally, you read the bits from right-to-left. Here are some other decimal numbers in binary: 0001 = 1 0010 = 2

using references to point to sliding window array in Perl

痞子三分冷 提交于 2020-01-02 08:02:12
问题 here is my problem: I have 2 arrays. One is character array and represents a sliding window. Characters gets shifted from the beginning and pushed at the end. I would like to use a second array to store references to array slices that 'follow' the characters as they move along. Example: my @char_array = ('h','e','l','l','o','w','o','r','l','d'); my $char_arr_ref=[@char_array[1..$#char_array]]; print @$char_arr_ref, "\n"; # slice contains 'elloworld'; shift(@char_array); push(@char_array), 'x'

using references to point to sliding window array in Perl

半腔热情 提交于 2020-01-02 08:02:11
问题 here is my problem: I have 2 arrays. One is character array and represents a sliding window. Characters gets shifted from the beginning and pushed at the end. I would like to use a second array to store references to array slices that 'follow' the characters as they move along. Example: my @char_array = ('h','e','l','l','o','w','o','r','l','d'); my $char_arr_ref=[@char_array[1..$#char_array]]; print @$char_arr_ref, "\n"; # slice contains 'elloworld'; shift(@char_array); push(@char_array), 'x'

const char* and free()

空扰寡人 提交于 2020-01-02 04:15:14
问题 Given the next code example, I'm unable to free the parameter const char* expression : // removes whitespace from a characterarray char* removewhitespace(const char* expression, int length) { int i = 0, j = 0; char* filtered; filtered = (char*)malloc(sizeof(char) * length); while(*(expression + i) != '\0') { if(!(*(expression + i) == ' ')) { *(filtered + j) = *(expression + i); j++; } i++; } filtered[j] = '\0'; free(expression); //this doesn't seem to work return filtered; } Before I return

Making an Array to Hold Arrays of Character Arrays in C

妖精的绣舞 提交于 2019-12-19 12:39:27
问题 My C is a little more than rusty at the moment, so I'm failing to create something I think should be pretty basic. Allow me to refer to character arrays as strings for this post. It will make things clearer for both me and you. What I have is an array that can hold 1 or more strings. For instance {"ab", "cd", "ef"}. I want to make another array to store multiple versions of the array of strings. So something like {{"ab", "cd", "ef"}, {"gh", "ij"}, {"kl"}}. What I have right now is: char

Making an Array to Hold Arrays of Character Arrays in C

自闭症网瘾萝莉.ら 提交于 2019-12-19 12:39:20
问题 My C is a little more than rusty at the moment, so I'm failing to create something I think should be pretty basic. Allow me to refer to character arrays as strings for this post. It will make things clearer for both me and you. What I have is an array that can hold 1 or more strings. For instance {"ab", "cd", "ef"}. I want to make another array to store multiple versions of the array of strings. So something like {{"ab", "cd", "ef"}, {"gh", "ij"}, {"kl"}}. What I have right now is: char

How do I return a variable size string from a function?

一个人想着一个人 提交于 2019-12-13 20:15:52
问题 I need a working code for a function that will return a random string with a random length. What I want to do would be better described by the following code. char *getRandomString() { char word[random-length]; // ...instructions that will fill word with random characters. return word; } void main() { char *string = getRandomString(); printf("Random string is: %s\n", string); } For this, I am strictly forbidden to use any other include than stdio.h . Edit: This project will be adapted to be

Passing a 3D array to a function.

▼魔方 西西 提交于 2019-12-13 08:19:32
问题 I'm having a hard time passing a 3D array to a function. I've googled it to death and I think I understand but the code crashes with no output when run. (codeblocks, gcc) #include <stdio.h> #include <stdlib.h> void foo(char (*foo_array_in_foo)[256][256]); int main() { char foo_array[256][256][256]; int line_num = 0; printf("Hello world!\n"); foo(foo_array); return 0; } void foo(char (*foo_array_in_foo)[256][256]) { printf("In foo\n"); } 回答1: You have a stack overflow 256*256*256 = 16777216

C++ convert strings to char arrays

天涯浪子 提交于 2019-12-07 12:02:30
问题 I want to have 2 strings as input so I can use getline(cin,s) (so I can pick the whole line until '\n' ) and then I want to search the second array if it contains the word of the first array without using string::find() or strstr() . But I still can't find a way to either convert strings to arrays int main() { string s; string s2; char array[50]; char array2[50]; cout<<"Give me the first word"<<endl; getline(cin,s); cout<<"Give me the text"<<endl; getline(cin.s2); array=s; array2=s2; } The

using references to point to sliding window array in Perl

喜夏-厌秋 提交于 2019-12-06 03:54:00
here is my problem: I have 2 arrays. One is character array and represents a sliding window. Characters gets shifted from the beginning and pushed at the end. I would like to use a second array to store references to array slices that 'follow' the characters as they move along. Example: my @char_array = ('h','e','l','l','o','w','o','r','l','d'); my $char_arr_ref=[@char_array[1..$#char_array]]; print @$char_arr_ref, "\n"; # slice contains 'elloworld'; shift(@char_array); push(@char_array), 'x'; print @$char_arr_ref, "\n"; # slice still contains 'elloworld', not 'lloworldx' as I need; IN other