Hot answers tagged

44 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 ...
user avatar
17 votes
Accepted

Array Initializer Must Be an Initializer List or String Literal

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 ...
user avatar
  • 17.4k
7 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] = ...
user avatar
  • 1,359
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 ...
user avatar
  • 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 ...
user avatar
  • 17.4k
4 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 ...
user avatar
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 ...
user avatar
  • 17.4k
4 votes
Accepted

PSET8 Can't add property to the object array

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 = ...
user avatar
  • 18.3k
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 ...
user avatar
  • 17.4k
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 ...
user avatar
  • 63.1k
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          ...
user avatar
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) ...
user avatar
  • 5,172
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 ...
user avatar
  • 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 ...
user avatar
  • 7,326
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 ...
user avatar
  • 18.3k
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) ...
user avatar
  • 239
2 votes
Accepted

How to Form a Single Integer from the Elements of an int Array?

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

How Does sizeof() Work?

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 ...
user avatar
  • 17.4k
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 ...
user avatar
  • 18.3k
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 ...
user avatar
  • 63.1k
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 ...
user avatar
  • 63.1k
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 ...
user avatar
  • 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 ...
user avatar
  • 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 ...
user avatar
  • 18.3k
2 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 ...
user avatar
  • 63.1k
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.
user avatar
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 ...
user avatar
  • 63.1k
2 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(). ...
user avatar
  • 63.1k
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[...
user avatar
  • 7,326
1 vote

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

Why not input the number as a string to begin with? (This would also make it easier to check the length is right - you seem to be comparing the size of the number, rather than its length, with 10). ...
user avatar

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