Hot answers tagged

45 votes
Accepted

Difference between char and char* in c

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 ...
8 votes

How to convert the digits of a number into elements of array?

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] = ...
  • 1,379
7 votes
Accepted

not fully understanding dynamically sized array in server.c pest

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 ...
  • 17.4k
6 votes

pset7: understanding php arrays

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 ...
  • 17.4k
5 votes

Difference between char and char* in c

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 ...
4 votes
Accepted

Pset 2 Initialise: "result of comparison against a string literal is unspecified"

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 ...
  • 64.7k
4 votes

about strings and arrays

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 ...
  • 17.4k
4 votes
Accepted

Comparing arrays in C

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 ...
  • 17.4k
4 votes
Accepted

Pset4 Resize.c Array Overstepping

After edit: For reference, here is the pseudocode from the walkthrough: for each row       for each pixel          ...
4 votes
Accepted

pset2 caesar converting from ascii to alphabetical

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) ...
  • 5,181
3 votes
Accepted

Why can we pass a struct variable to a function by value but not an array?

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 ...
  • 17.4k
3 votes
Accepted

Why is this program behaving unexpectedly?

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 ...
  • 7,356
3 votes
Accepted

Create an array for a case statement - confused

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 ...
  • 18.4k
3 votes
Accepted

trail of strange characters after printing character array

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 ...
  • 64.7k
3 votes
Accepted

Pset4 - Recover (usage of sprintf)

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) ...
  • 239
3 votes
Accepted

Week 2 passing array example problem?

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(). ...
  • 64.7k
2 votes
Accepted

array error: expected identifier or '('

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 ...
  • 18.4k
2 votes

array error: expected identifier or '('

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 ...
  • 64.7k
2 votes
Accepted

c prog - Generate ''ALPHABET'' and ''alphabet'' in arrays, wrong output

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 ...
  • 64.7k
2 votes
Accepted

declaring char array vs malloc

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 ...
  • 17.4k
2 votes
Accepted

how effective would it have been to use an array for the dictionary?

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 ...
  • 17.4k
2 votes
Accepted

How to use fseek along side arrays? Pset4 recover

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 ...
  • 18.4k
2 votes
Accepted

Problem accessing the last element of the array

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 votes
Accepted

array size in C

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 ...
  • 64.7k
1 vote

Array in php vs R (pset7 sell.php)

Just like _POST your positions is an associative array. Just as you get $_POST["symbol"] you get $positions["price"]. If I understood correctly you want to do something like the following <?php ...
  • 7,356
1 vote
Accepted

2nd Week and 2nd Lecture: Dynamic array

What you want to say, is that the array is immutable, which means once you have declared their length, you can't change it to add more values. A possible solution to the problem you describe are ...
  • 7,356
1 vote
Accepted

How to iterate through an array using pointers

Yes you are correct. The compiler knows what data type you are using and traverses by the correct amount of bytes every time. You just tell it how many places away from zero you want (by the + i). You ...
  • 7,356
1 vote
Accepted

Why can't we declare an array dynamically?

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[...
  • 7,356
1 vote

Struct array not assigning value being set

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 ...
  • 17.4k
1 vote
Accepted

Help me find the bug in this array code

you have your second for loop nested inside the first one which means that the second for loop is not executed after votecounter[1] to votecounter[4] are initialized to 0, but rather it's executed ...
  • 17.4k

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