28

The difference 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 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[] you are ...


9

First, recall that pointers are variables which contain an address to some other variable. So if you have a variable int* n, this defines a variable n, which contains the address of some place in memory of where to find an integer. Each location in memory is given a unique number. Pointers are numbers which refer to locations in memory. So a pointer with ...


6

Your problem is that letters[i] is a char, but the crypt() function expects a char * (string) as its first argument. So what you really want to do is pass a string with a single character, not a single character. You need to pass this char array ['a', '\0'], not this char 'a'. A simple fix can be to declare a char array with a length of 2, with the second ...


6

OK, this is going to be long answer, because what you've asked spans many subjects, so bare with me. Arrays vs Pointers First, let's learn the difference between arrays and pointers. When you declare an array as: int nums[20]; you reserve 20 consecutive places in memory, which you can refer to by the variable nums. Specifically, you reserve these places ...


5

what's going on under the hood? when having char *s = "hello"; basically an array of chars is created for you and is initialized with the contents of the string literal (in this case, "hello"). The address of the location, where this array is stored, is then stored into s. Technically, this is different because this is a syntactic sugar that C provides ...


4

What is the difference between char word[length] and char *word = malloc(sizeof(char) * LENGTH)?? A char * is a pointer to a char (or a sequence of chars -- aka a string) while a char[] is an array of chars. They're both interchangeably used, but here are some main differences. If you use a char *, as a member of a struct, you have to allocate memory for ...


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

Regardless of whether the code does what it should do or not, the problem is likely to be caused by the fact that you are trying to access cursor->next when cursor is actually a null-pointer. This would be the situation in case tabla[index] was the only node in this linked-list and newnode->word does not equal tabla[index]->word which would mean ...


2

You are confusing things here. First, according to the manual page of realloc Unless ptr is NULL, it must have been returned by an earlier call to malloc(), calloc() or realloc(). That is obviously not the case here since the member word of the struct node is NOT returned by a call to any of these functions (but new_node itself is). you can't ...


2

This question was also posed on Facebook by you here: https://www.facebook.com/groups/cs50/permalink/343562319124153/ In a nutshell, GetString() mallocs space to hold the character array that you then enter. Any "definitely lost" bytes that valgrind reports are from that GetString() malloc unless you specifically free it using free(s); or free(ptr->str)...


2

You can either do: char* str = "world"; In this case, world automatically will be stored in some chunk of memory and the adress of the first byte of this chunk will be stored in str. Using this metod the null characters is automatically added at the end of the string. or: char str[6]; In this case, a chunk of memory will be reserved to store 6 ...


2

I originally didn't alloc for the struct, just declared it. It worked, and from what I understand, it worked because it was created in the stack? Am I correct? the type BITMAPINFOHEADER is not a pointer type. it is similar to other native types (e.g., int, char, etc) except that it is a little more complex. when declaring a variable of that type, you ...


2

why do you use int* array[n]; and not int array[n];? you aren't calling your sort() function.


2

It turns out that using double quotes vs single quotes makes a difference in C! Double quotes are used to wrap a STRING, while single quotes are used to wrap a CHARACTER. This is easy to remember if you just think about single quote, single char. Replacing if (input[i] == " "){ with if (input[i] == ' '){ Fixed the problem. reference: https://...


2

From the inside out: (arr + i) is a pointer to the i'th element of arr *(arr + i) is the contents of the pointer to i'th element of arr (dereferenced (arr + i)) &(*(arr + 1) is the address of the contents of the pointer to i'th element of arr (which is the same "address" as (arr + i)) Maybe you got to this point because scanf("%d", arr[i]); wouldn't ...


2

You don't need to specify the return type of your function when you call it, only when you declare/define it. So you've declared the function right, like this: void swap (int* a,int* b) But when you call it, you have to omit the type specifications, like this: swap(&a,&b); But what you've done is this: void swap(&a,&b); TL;DR: remove the ...


2

*= and += (same for many other operators) are combined assignment operators. a += b is like a = a + b, a *= b is like a = a * b, and so on. In the line newbi.biWidth = bi.biWidth *= n; you also use the fact that an assignment itself is an expression of the assigned value (for *= that's the product). So here you multiply-assign bi.biWidth with n (changing bi....


2

What is ! = 2? Does not look like a valid expression to me. Use if (argc != 2). Instead of the other ! = 0, something like if (strlen(plaintext) != 0) would be correct, this condition would always be true at that point, since you can leave the do..while loop only if it is. But I don't think you're supposed to do a do..while loop. I could imagine an empty ...


2

printf("%c\n", *s); is the same as printf("%c\n", s[0]); By definition of printf, the %c format specifier in printf expects a character. So, *s says go to the address stored in s, and give me the character there. The square bracket notation says "get the address of s and add 0 to it, then dereference it to get its value." printf("%s\n", s); By ...


2

1 It's not a single char. You've declared an array of 8 chars, which is what you need to hold "000.jpg" {'0', '0', '0', '.', 'j', 'p', 'g', '\0'} 2 bookmark is an array. An array will "decay" to a pointer type when needed. So you can use either. 3 %03d the 03 means you want a 3-digit number which will use leading zeroes. Using d vs i makes no ...


2

buffer is an array. Array variables "decay" into pointers when needed, so fwrite will understand buffer to mean a pointer to the first element (ie, the same as &buffer).


2

*a is same as a[0], pointing to the first element of a. Generally, *(a + i) is same as a[i]. I don't think you want an array of strings (string is a typedef of char*) all pointing to the same variable (and it's dangerous to use %s with &b, as you don't know where the next zero byte is!), I assume you want an array of char instead (don't forget space for ...


2

string out[n]; You've declared an array of strings with this declaration. What you probably want is one string (ie, an array of chars). Change that to char out[n]; to get one string.


2

Look at exactly what this code does: void setCharacter(char* pCharacter) { pCharacter = malloc(sizeof(char)); *pCharacter = 'b'; } Remember that things get passed by copy. First, pCharacter is an address that's passed from main to setCharacter. Then setCharacter changes that address by malloc'ing new memory and assigning the address of that ...


2

There are several issues and areas for improvement here. First, there's this: FILE *f = fopen("argv[1]", "r"); By putting argv[1] in quotes, you're saying to treat whatever is in quotes as a literal string. In other words, open a file named "argv[1]". Remove the quotes to use the string/filename contained inside argv[1]. That ...


1

first, I wanna make it clear that there is a difference between the address of a pointer and the address stored in a pointer (aka the address that the pointer points to). pointers are special type of variables that can store memory addresses. where does a pointer store an address? in a memory location somewhere that also has an address. int i = 10; // ...


1

Notice the difference between the declaration of abs_path/query in main char abs_path[LimitRequestLine + 1]; char query[LimitRequestLine + 1]; vs. the declaration in parse. bool parse(const char* line, char* abs_path, char* query) In main they are char arrays. In parse they are char*. Both your sample outputs should be expected. Both variables appear ...


1

There are two major problems in the code. First, look at this: node* root = (node*) malloc(sizeof(node*)); The parameter for malloc is the size of the object that needs to be allocated. Unfortunately, by adding the asterisk in the sizeof() call, you are allocating just enough memory to store a pointer to a node, not an actual node, which is much larger. ...


1

It would have helped if you had told us exactly what the compile errors were. However, with a cursory look at the code, I see this: free *p; Your usage of free() is incorrect. For starters, it needs parentheses. Next, it doesn't need the *. To free the memory pointed to by p, the correct form in this case would be free(p); If there are other issues, ...


1

For any variable/identifier x, &x is a way to get the address of the memory block where it is stored. Lets take a deeper look, Consider we have declared and initialized int a = 10; For any data in any language, there are 3 sorts of things associated with it : The Data Value - Its the value of the entity you are dealing with. In the above example, we ...


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