Hot answers tagged

34

The difference between char* the pointer and char[] the array is how you interact with them after you create them. If you are just printing the two examples, it will perform exactly the same. They both generate data in memory, {h, e, l, l, o, /0}. The fundamental difference is that in one char* you are assigning it to a pointer, which is a variable. In char[]...


17

First, when declaring a char array, you've to specify its size within the brackets like: char arr[10]; except when you're initializing it with a string literal. And since we don't know the number of characters the user will enter beforehand we may consider that GetString() takes care of that and returns a string (a char *). You may just declare a string ...


7

of course needle and haystack both are pointers and thus they both store addresses which are essentially numbers so we can perform arithmetic operations on them. needle - haystack + 2 + 1 subtracts the address that haystack stores from the address that needle stores (not the other way around) then adds 3. now what is that and why is it done that way? in ...


6

If you want to store each digit of a number as an element of an array you can do the following. long long number = 1234567890; int digits[10]; for (int i = 0; i < 10; i++) { digits[i] = number % 10; number = number / 10; } // At the end digits = {0, 9, 8, 7, 6, 5, 4, 3, 2, 1} But if you want the elemnts to keep the same order than the original ...


6

First, after WHERE you should specify column name(s). So this call to query() $rows = query("SELECT * FROM `stocks` WHERE $id"); should be like that $rows = query("SELECT * FROM stocks WHERE columnName = ?", $id); Second, the function query() returns a number of rows, at the first place, where each row is composed of columns. Meanwhile, if you tried to ...


4

Have a look at memcmp()! Also, you can define your own function bool compare(datatype array1[], datatype array2[], size_t size1, size_t size2) { // check whether they're both of the same size if (size1 != size2) { return false; } // compare for (int i = 0; i < size1; i++) { if (array1[i] != array2[i]) ...


4

I'm not sure you can simply initialise the new array like that. Why not just add the placemark and marker to the PASSENGERS array? You can just do that 'on the fly' with PASSENGERS[i].placemark = placemark; etc. Anything you want to store in the passengers array can be done like that. For example, I have a field that holds whether or not the passenger is ...


4

Good question! GetString uses a slightly different technique, namely dynamic memory allocation. Using dynamic memory allocation, you can allocate and/or free memory at run time (i.e., as the program is running). You'll get exposed to that technique as you proceed with the course. For now, what you should probably know is that basically GetString allocates ...


4

It's usually the subtle things that give people the most trouble until they're understood. if (name[i] == (int)" ") You don't realize it, but you've made a simple error. You want to compare a char to a char, but you are actually telling the compiler to compare a single char to a string. The problem is that you are using double quotes. Double quotes ...


4

After edit: For reference, here is the pseudocode from the walkthrough: for each row       for each pixel          write to array n times       for n times          write array to outfile  &...


4

Your formula is correct as well as the understanding of the formula, only that you are mixing two things in a wrong way, look carefully at the following expression: ((('s[i]' - 97) + n) % 26) + 97) What is s[i], and what is 's[i]'? I have to say that it is the first time that I encounter this situation, probably 's[i]' is interpreted by the compiler as a ...


3

The problem is with your scanf and the fact that you are telling it it will read an int but you give it a char. ASCII values may be represented by integers, but scanf can distinguish them. It checks for each digit to see if it's in the range 48-57 which is '0'-'9' in ASCII. So when it reads a digit that is not in that range ('q' for example), it puts it back ...


3

C99 N1256 draft There are two different uses of character string literals: Initialize char[]: char c[] = "abc"; This is "more magic", and described at 6.7.8/14 "Initialization": An array of character type may be initialized by a character string literal, optionally enclosed in braces. Successive characters of the character string literal (...


3

If you know the color names, why not simply create an array of strings to hold them? char* color[] = {"RED","ORANGE","YELLOW","BLUE","GREEN","BLACK"}; Then, wherever you need to assign a color, you could just say newcolor = color[2]; //sets the string 'newcolor' to "YELLOW" Or if you are using that color in a function that takes a string: setColor(thing,...


3

Before I answer this question and important clarification must be made about what it means for something to "work". There is a term that will come up a lot when talking about C (and other languages) called "undefined behavior". Programs which invoke undefined behavior may compile just fine and may even appear to work just fine, but it is not guaranteed to ...


2

Assuming you don't want to add them and store the result in a new variable. To print them concatenated to each other like 15, you may write something like this. printf("%d%d\n", x, y); To add them and store the result in a new variable, you may multiply x by 10 first, then add them and store the result in a new int variable int sum = (x * 10) + y; printf("...


2

An int is 32 bits (4 bytes) on the appliance. The size of a 5-int array is 20 bytes (5 ints * 4 bytes each). The sizeof() operator is used to calculate the size of any datatype in bytes. For more information, you may read this wikipedia article!


2

That code on its own won't compile because you don't have a main function. In the video, she is asking the students to write the code that will initialize the array and then print it out ... but she isn't expecting that snippet to actually compile and run.


2

If this is your complete code, then you are missing some structural elements. The simple explanation is that there needs to be a main() function in your program to contain the code. You would also need the appropriate include statements for the functions (printf(), etc.) that you are using. It doesn't error out on the first line because the compiler ...


2

As taught we can pass a whole "struct" by value to a function but we can't pass an array by value. Why? the short answer is: because structs and arrays are implemented differently in C. longer answer: there is a difference between a struct type and its pointer type. for example, given the struct struct MyStruct {// members}; MyStruct s; // and ...


2

The problem is simple. You have created two string arrays. Each holds the 26 letter alphabet, one lower case, the other upper case. They are created in sequential memory. In this case, the UC array is first, followed immediately by the lc array. You then try to print them out as strings, using the base variable (i.e., uppercase and lowcase) as the ...


2

yes, indeed there is a difference! when you use malloc, the block of memory that you are trying to allocate gets allocated in a region of memory called the heap. freeing memory allocated on the heap has to be done manually (by calling free). when you declare an array of a certain size, depending on where you declare your array, the memory for it may get ...


2

suppose we are to implement speller using an array instead of a hash-table or a trie. a main problem that we have is that we don't know the number of words in the dictionary in advance (yes, we know the number of words in the given dictionary per the specs, but our program should allow loading custom dictionaries). as a result, we would have to ...


2

for(int i = 0; i < MEMORYBLOCK; i++) { //read & store 512bytes in memory[i] fread(&(store[i]), 1, 1, inptr); //do we need *inptr instead? } If you want to read 512 bytes into an array, then you can simply call fread once, like this: fread(store, sizeof(BYTE), MEMORYBLOCK, inptr); Also, you shouldn't need to use ...


2

Uhhuh. I see that it is doing a nice job of encoding and storing the encoded characters in an array. But have you also stored the end of string marker '\0' at the end of the string? printf will keep printing whatever it finds in memory until it finds that null character which marks the end of the string. If this answers your question, please click on the ...


2

If an array contains 27 elements and the first element is index 0, then the last element is index 26. The last index is always one less than the number of elements, because indexes start at 0.


2

In C, when you declare an array, it's size MUST be declared when it is created. There is no mechanism built into C for dynamic array RE-sizing. An array's size is declared explicitly by a statement like int array[12]; or implicitly by initializing it at creation. For example, int array[] = { 31, 25, 16, 5, 2, 2, 8, 21 }; would create an int array with 8 ...


2

When the code calls set_int(a), the value stored in x in main(), 10, is passed to set_int(). That "10" is a copy of the original and is stored in a local variable, x, inside of set_int(). Then, x is changed to 22 inside of set_int(). The important thing to remember here is this: The variable in main that held "10" will not be changed by ...


1

Yes you can. Say you want an array of integers, you can do it like that: printf("What is the length of the array?\n"); int n = GetInt(); int numbers[n]; for (int i = 0; i < n; i++) { numbers[i] = GetInt(); }


1

input_array is an array of pointers to inputs — input_array[counter] evaluates to an input * not an input. accessing input_value through input_array[counter] using the . operator is not actually the correct way to do that. you need to allocate memory for each of these pointers first maybe using malloc. see the short on structs for more on how you can access ...


Only top voted, non community-wiki answers of a minimum length are eligible