Questions tagged [array]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
18
votes
2answers
50k views

Difference between char and char* in c

suppose we declare two variable char s[]= "hello" char* p = "hello". what is the difference between these two?
6
votes
3answers
75k views

Array Initializer Must Be an Initializer List or String Literal

Given this array: char p[90] = GetString(); I get this error when compiling: error: array initializer must be an initializer list or string literal How to initialize this array properly?
3
votes
2answers
60 views

Why is this program behaving unexpectedly?

The program below prints a value of size 20 no matter how small input we provide. Why? #include <stdio.h> #define LENGTH 20 int main(){ int arr[LENGTH], size = 0; printf("Enter array ...
3
votes
1answer
5k views

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? And, what if the passed struct contains an array as a member? Would it get passed completely (...
2
votes
1answer
230 views

PSET8 Can't add property to the object array

In populate() function of service.js, I was trying to build an object array to store the passengers' name, house, placemark and marker. Basically, I created a global array, say database[], then when ...
2
votes
2answers
126 views

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

I just completed pset5 using a trie, but I'm curious about other methods. How effective would it have been to load the dictionary into an array and then use a binary search function to locate words ...
2
votes
3answers
829 views

What stuff that's inside array[-1] or array[n + 1]?

Here a portion of the loop I used to implement insertion sort algorithm: while (j > 0 && values[j - 1] > actualElement) { // place the unsorted element inside the correct position ...
2
votes
1answer
179 views

PHP array syntax

I am confused about why I can't print the content of $positions using the code below. $rows = query("SELECT * FROM protfolio WHERE id = ?", $_SESSION["id"]); $positions = []; foreach ($rows as $row) ...
1
vote
1answer
139 views

not fully understanding dynamically sized array in server.c pest

I am not fully understanding what is happening in the line below Char line[needle - haystack + 2 + 1] Is it subtracting the address of needle from haystack(haystack and needle are pointers) then ...
1
vote
1answer
360 views

pset7: understanding php arrays

I'm having a lot of trouble understanding these arrays in php. Here I want to query SQL to get me the value of cash of the user. Then I want to add it to the $position variable that is later sent to ...
1
vote
1answer
426 views

Pset4 Resize.c Array Overstepping

I have been stuck on resize for a while. I am currently struggling on the vertical portion of resize. I am using the "rewrite" method as explained in the problem spec. My program does resize an image ...
1
vote
1answer
315 views

about strings and arrays

while declaring arrays we have to mention size of arrays as arrays cannot be dynamic similarly string is array of chars how can we not mention size of array we simply use Getstring() or scanf
1
vote
1answer
235 views

array size in C

I have question regarding array size declaration in C. I see that if I declare an array in the main function without specifying its size like int arr[]; arr[0] = 7; arr[1]= 8; it shows an error ...
1
vote
2answers
739 views

declaring char array vs malloc

I was watching the week 5 lecture and David said that a chunk of memory can be allocated two ways: using malloc(); declaring an array of a certain size (char buffer[10]). Is there really any ...
1
vote
1answer
40 views

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

Given an array of ints int arr[] = {2, 0, 1, 4}; and I want to manipulate them so that I get the value 2014 and store it in an int variable How can I do that?
1
vote
1answer
120 views

Pset2: Copying string contents

I just can't seem to get this right. The example in the week 2 short Arrays is as follows: int foo[5]{1,2,3,4,5}; int bar[5] for(j=0;j<5;j++) { bar[j]=foo[j]; } This does not seem to work with ...
1
vote
1answer
36 views

Passing SQL values into arrays

I'm designing a budgeting website for the final project. One of the features that I want to implement is to be able to track how much money you've spent for each item category. The SQL command that I'...
1
vote
2answers
95 views

Array in php vs R (pset7 sell.php)

Ok, I'm at pset7 (C$50 Finance), more specifically trying to implement sell.php. I have created a template, sell_form.php, where users can select the symbol of the stock they want to sell and ...
0
votes
2answers
16k views

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

For example in pset1 hacker edition program asks user to input the number of credit card. Imagine that a digits are: 1234567890 So can anyone tell me does it possible to convert this number into ...
0
votes
2answers
2k views

pset2 caesar converting from ascii to alphabetical

#include <stdio.h> #include <cs50.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(int argc, string argv[]) { if (argc == 2) { int ...
0
votes
1answer
566 views

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

I've been having this persistent issue that's taken me a day of googling and referencing and still have no idea what's up. Essentially, the code below (which is hopefully commented well enough to make ...
0
votes
2answers
877 views

array error: expected identifier or '('

I've just copied a code from a slide explain array creation, but from a lot of tries all come back the same errors "expected identifier or '('". Follows the code: int example [] = { 1, 2, 3 }; for (...
0
votes
1answer
135 views

Comparing arrays in C

Is there a good way (or function) to compare all elements of two arrays (or selected elements) without hardcoding (i.e., while(a[1] == b[1] && a[2] == b[2] && ...)) and without using a ...
0
votes
1answer
1k views

Pset4 - Recover (usage of sprintf)

When I am working on Pset4 - Recover, I realize I do not understand the concept of array and pointer. Although I finally worked it out, I would be very grateful if you guys can explain the following: ...
0
votes
1answer
57 views

Create an array for a case statement - confused

I am a bit stuck on how to create an array so i can create a string to declare a colour. I have the following code: char[7] color; //biggest color: yellow (6) + \o (1) = 7 for(int i = 0; i < ...
0
votes
2answers
845 views

How Does sizeof() Work?

let suppose I have int coin[5] = {25, 10, 5, 1, 3}; printf("%i\n", sizeof(coin)); Please, I am not understanding why this is giving me an output of 20 I am still doing pset1 for the greedy ...
0
votes
1answer
34 views

Week 2 passing array example problem?

Hi, I have problem understanding the concept of 'passing an array by reference instead of value' and I cant understand this example. Can anyone explain to me why does it take 10, 22 as final answer?
0
votes
1answer
315 views

Problem accessing the last element of the array

I've decided to implement the load function using trie, which I've defined as typdef struct trie { struct trie letters[27]; bool isWord; }trie; then later, when I try to ...
0
votes
1answer
807 views

trail of strange characters after printing character array

I wrote my vigenere cipher program and everything works fine, but when I run check50 I get the following error:(p is the plaintext string) char c[strlen(p)]; for(int i = 0, j = 0, l = strlen(p), n = ...
0
votes
1answer
209 views

How to use fseek along side arrays? Pset4 recover

I'm re-wrote this code and did not get any output or error. Tried valgrind and nothing was leaked. I suspect it's either: 1) my fseek is not moving my cursor in inpt 'card.raw' 2) and/or.. because ...
0
votes
2answers
3k views

Excess elements in scalar initilizer?

#include <cs50.h> #include <stdio.h> int example[]; int main(void) { int example = {1,2,3}; for (int i = 0; i < 3; i++) { printf("%i \n", example[i]); } } Why ...
0
votes
1answer
369 views

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

I'm trying to generate two independent characters arrays. The first one must contain the alphabet in lowercase and the second one, in uppercase. My code works but in the second case, the output ...
0
votes
2answers
389 views

How to check string argv[] for errors?

hope I can get some help here. I'm only at week 3 in CS50 and wanted to play around with command-line arguments a bit before moving on. I got this working (diagonalizing input), but couldn't figure ...
0
votes
2answers
22k views

How to create an array/subarray by extracting a part of another array?

Does anyone know how one can create a subarray by extracting a part of an existing array. Is there something like subarray[] =existingArray[0-3]? I need this to compare the first 4 bytes of my block ...
0
votes
1answer
17 views

Why is an Array longer then the input?

why we can access more information than the lenght of the array? I thought that after the Null (0) the array ends. Why we can access position 40?
0
votes
1answer
14 views

When I'm creating array

This has me confused. In the following code, I create an array of characters initialized at size 7. The array needs to store at least 6 characters, so I've made it large enough to do that (I think?). ...
0
votes
2answers
48 views

Caesar : Extra random characters at the end of a char ciphertext array

I'm having a problem with an output. Sometimes output adds an extra characters, different each time the code runs. For example, when I use key "1" and write "a", it displays "...
0
votes
1answer
15 views

How can i arrange the output in decending order In this program? [closed]

#include <stdio.h> int main() { int n,i; printf("Enter a number: "); scanf("%d",&n); for (i=0; i<=n; ++i) { if (i%(7*13*19)==0) { ...
0
votes
1answer
30 views

Having trouble identifying non-decimal digit in Caesar PSET 2

I'm working on Caesar from Problem Set 2. I have done the first step to check for a single command line argument, but I'm stuck on how to check the argument to see if any characters are non - decimal ...
0
votes
1answer
57 views

My code for Caesar gets segmentation error when executed, but in debugger it works fine

I'm getting a segmentation error as soon as I execute this program. I've run it through the debugger and it works fine and outputs the correct ciphertext. There's gotta be something going on in the ...
0
votes
1answer
247 views

check50 claiming pairs are sorted incorrectly

On Problem Set 3, check50 has flagged my program as not having sorted the pairs correctly. However, the pairs array appears to be sorted in every case I've tried. I used insertion sort and had ...
0
votes
1answer
32 views

In pset2, Vigenere, I made and used an array that I would like for it to be explained

I’ve successfully solved Caesar and Vigenere pset but made and used an array that I stumbled upon through trial and error but don’t exactly know why and how it worked. I’m hoping someone could clarify ...
0
votes
1answer
56 views

Pset2 Vigenere, array with garbage

I am getting some garbage values when declaring my array. I kinda understand that when declaring without initializing it provides garbage values, but I don't understand why the array is larger than ...
0
votes
1answer
74 views

Caesar not getting output

I'm stuck troubleshooting what is wrong with my code. It prompts for plaintext correctly, but when entered returns a blank "ciphertext: ". # include <stdio.h> # include <cs50.h> # include ...
0
votes
1answer
27 views

Why string variable has no square bracket?

Though it's said that string is an array of characters why string variable does not have square brackets on the immediate right? In CS50 we call string as string s why not as string[]? then how can ...
0
votes
1answer
34 views

Pset4 - Array declaration- Heap

In shorts of Pset4 it was mentioned, that pointers are created in heap. Does it mean, that all arrays are stored in heap?
0
votes
1answer
87 views

Pset5 Dictionary C Struct Array Call In Load Function Issue

I've defined the struct as following: // Node struct typedef struct node { // Node value char word[LENGTH + 1]; // Node Pointer to next value/node struct node* ...
0
votes
1answer
2k views

I'm getting a runtime error on an array and I'm not sure why:

The following code Compiled but gave me an error (credit.c:21:7: runtime error: index 15 out of bounds for type 'int [15]') on the last line included while running: int m; int r; int counter; int ...
0
votes
1answer
57 views

pset vigenere skipped over if statement

for (int i = 0, j = strlen(s), k = 0; i < j; i++, k++) { if (isalpha(s[i])) { if (isupper(s[i])) { if (argv[1][k] ...
0
votes
3answers
202 views

pset2 vigenere shifting characters by the same

for (int i = 0, j = strlen(s), k = 0; i < j; i++, k++) { if (isalpha(s[i])) { if (isupper(s[i])) { if (argv[1][k] != '\0') { printf("%...