Skip to main content
8 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 ...
Ciro Santilli OurBigBook.com's user avatar
7 votes
Accepted

Pointers and arrays, don't understand the relationship

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 ...
ChrisG's user avatar
  • 7,396
6 votes
Accepted

PSET2: CRACK vs POINTERS

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 ...
ChrisG's user avatar
  • 7,396
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) ...
chad's user avatar
  • 239
3 votes
Accepted

Why can you say the name of an array is a pointer to the first element of the array in C?

First I commend you for asking the question and trying to affirm your understanding of the language and what you are learning. It is admirable and requires confidence and a degree of bravery. And it ...
UpAndAdam's user avatar
  • 1,426
2 votes
Accepted

What happens when you assign a character string to a char* instead of writing element by element?

I'll let you visualize this for yourself. Go here http://www.pythontutor.com/c.html#mode=edit and paste your code, then hit "visualize execution". You'll see graphically what's happening in memory. ...
Yuri Laguardia's user avatar
2 votes

Why does this snippet of code work?

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 ...
DinoCoderSaurus's user avatar
2 votes
Accepted

pset4 resize file header question

*= 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 ...
Blauelf's user avatar
  • 21k
2 votes
Accepted

I have written this swap code but it is not compiling, Where's the bug

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 ...
Yuri Laguardia's user avatar
2 votes
Accepted

C: Vigenere - incompatible pointer types passing 'char **' to parameter of type 'const char *

The error comes from here: for(int l=0; l<strlen(argv); l++) Think about it. strlen() wants a const char* or char*. You gave it argv, which is an array of the command line arguments. Because argv ...
SuperNovaCoder's user avatar
2 votes

Error with comparison between pointer and integer

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 ...
Blauelf's user avatar
  • 21k
2 votes
Accepted

Difference between s and *s in pointer

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 ...
curiouskiwi's user avatar
  • 18.7k
2 votes

pointers and scanf : when do I use &x, *x or just x?

I think you got it. scanf needs a memory address so it knows where to write to. Whenever you add & you get the memory address of what follows. But in case of strings, what we pass as a string ...
Blauelf's user avatar
  • 21k
2 votes
Accepted

recover pset4 file handling

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 ...
curiouskiwi's user avatar
  • 18.7k
2 votes
Accepted

Using * and & both works

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).
curiouskiwi's user avatar
  • 18.7k
2 votes
Accepted

Printf doesn't print whole string "11111"

*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 ...
Blauelf's user avatar
  • 21k
2 votes
Accepted

vigenere output "incompatible pointer types"

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.
curiouskiwi's user avatar
  • 18.7k
2 votes
Accepted

Pointer passed as argument, then suddenly goes null

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, ...
Cliff B's user avatar
  • 69k
2 votes
Accepted

I know that my recover is wrong, but I'm not sure what steps to take-

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 ...
Cliff B's user avatar
  • 69k
2 votes
Accepted

PSET2 Caesar - Stuck at 'isdigit' step: Do I need to learn about pointers, or is something else going on?

Its a simple and common issue with new programmers. ;-) Yes, argv[] is an array of strings. So, argv[1] is a string. Now, say that argv[1] contains the string "cat". Then argv[1][0] ...
Cliff B's user avatar
  • 69k
2 votes
Accepted

incompatible pointer types error

create_family is the name of your function. In order to call it, be sure you don't forget the ( ) with the argument. If I wanted to create a family with generations number of generations, then I'd ...
curiouskiwi's user avatar
  • 18.7k
2 votes
Accepted

PSET4 Blur not passing Check50

Without doing a deep dive, I do see a major problem in the logic. The code copies all the pixels from image to temp array. But then, it does all the calculations using the temp array as the source ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

Pset 5 Speller Check/Load Entries in Hashtable All Have Same Memory Address

The proximate problem is free(newNode);. You are building this hashtable with addresses to memory that need to persist for the life of the program. That's the essential point of a separate unload ...
DinoCoderSaurus's user avatar
1 vote
Accepted

Declaring an array of structure pointers

It is the same as everywhere else. If your struct is for example struct node, an array of pointers would be for example struct node *myarray[ARR_LENGTH] (assuming ARR_LENGTH to be a constant meaning ...
Blauelf's user avatar
  • 21k
1 vote
Accepted

pointer for function using global variable

Interesting question. The answer is that a global variable does not work in your program. The compiler warning is really telling you that the variable a, in main, is different from the variable a in ...
MARS's user avatar
  • 5,169
1 vote
Accepted

Week 5 - Speller. Instance remaining as NULL after setting inside function

Function parameters in C are usually passed by value, creating a variable that's initialised to the passed value. That means your assignment to tree changes the variable tree in addWordToTree, but ...
Blauelf's user avatar
  • 21k
1 vote
Accepted

incompatible pointer to integer conversion

The problem is your declaration of name as an int. get_string tries to return a string (or char array). It can't store the string ( a char array )in an int. The second error is similar. Since name ...
Cliff B's user avatar
  • 69k
1 vote

Pointer variable

On 32 bit systems such as IDE50 memory addresses (pointers) are 32 bits as the name would suggest; likewise on 64 bit systems the memory addresses are 64 bits. 32 / 8 = 4 64 / 8 = 8 So a pointer is ...
kluvin's user avatar
  • 1,223
1 vote

De-referencing an array of pointers to structures

It's a cruel trick by the evil computer!! It's likely that you do understand "all that stuff". The real problem may be scanf! I tried a little repro case, couldn't get it to work (it didn't prompt as ...
DinoCoderSaurus's user avatar
1 vote
Accepted

PSET6 - load function - 1 file is not loaded (the jpg)

The primary point of failure is here memcpy(*content, buffer, ret);. You are always copying buffer into the first byte of content. Since most of the test files are less than 512 bytes..... This ...
DinoCoderSaurus's user avatar

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